Python - tkinter:打开和关闭对话窗口(Python - tkinter: Opening and closing dialog windows)
我对Python非常陌生,不得不写一个简单的GUI程序,为了简单起见,我选择在tkinter中做这个程序。
我想要的图形用户界面应该与在Windows上安装程序时经常遇到的对话框非常相似(您想在哪里安装,需要什么模块等)。 基本上,当它在python3.3中运行时,我想要一个窗口出现,其中一些选项占据了大部分窗口,底部的'next','back'和'cancel'按钮; 通过点击'下一步'按钮,当前窗口关闭,并打开一个新窗口,除了它有不同的选项(或者它是同一个窗口,但其内容已被销毁,我不确定会更好)。 我想要的粗略布局显示在此图像中
我查看了一些代码,它们做了类似的事情,但一直没能找到任何代码。 我已经看到了这个答案 ,但这不是我想要的。 我已经使用这个教程来了解我对tkinter的了解,但是我无法找到答案。
下面是我想要做的简化版本的极其糟糕的尝试:当我运行代码时,它会创建一个带有两个按钮的窗口。 “退出”按钮正常工作; 然而,当我点击“下一步”按钮时,它会关闭窗口并根据需要打开一个新窗口,但它也会打开另一个窗口。
from tkinter import * from tkinter import ttk def win1(): mainframe = ttk.Frame(root, padding = '3 3 12 12') mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S)) mainframe.columnconfigure(0, weight = 1) mainframe.rowconfigure(0, weight = 1) ttk.Button(mainframe, text = 'Next', command = win2).grid( column = 1, row = 1, sticky = W) ttk.Button(mainframe, text = 'Quit', command = quit).grid( column = 1, row = 2, sticky = W) root.mainloop() def quit(): root.destroy() def win2(): quit() new = Toplevel() new.title('Window 2') new = ttk.Frame(root, padding = '3 3 12 12') new.grid(column = 0, row = 0, sticky = (N, W, E, S)) new.columnconfigure(0, weight = 1) new.rowconfigure(0, weight = 1) ttk.Button(mainframe, text = 'Next', command = win2).grid( column = 1, row = 1, sticky = W) root = Tk() win1()
这给出了以下错误信息(我不明白):
Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.3/tkinter/__init__.py", line 1478, in __call__ return self.func(*args) File "<stdin>", line 23, in win2 File "/usr/lib/python3.3/tkinter/ttk.py", line 733, in __init__ Widget.__init__(self, master, "ttk::frame", kw) File "/usr/lib/python3.3/tkinter/ttk.py", line 553, in __init__ tkinter.Widget.__init__(self, master, widgetname, kw=kw) File "/usr/lib/python3.3/tkinter/__init__.py", line 2078, in __init__ (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: this isn't a Tk applicationNULL main window
除了它实际上并没有做我想做的事情之外,我觉得我正在以完全错误的方式去做(定义函数中的窗口等),并且当我想要遇到很多麻烦时使其更加复杂。 任何人都可以用更好的方式重写我的代码,并以一种方式帮助我构建更复杂的程序,提供资源来学习制作我想要的程序所需的内容,甚至提供建议? 谢谢。
I am very new to Python and have to write a simple GUI program, which I chose to do in tkinter for simplicity.
The GUI I want should be very similar to the dialog one often experiences when installing a program on Windows (where would you like to install, what modules would you like, etc). Basically when it is run in python3.3, I want a window to appear with some options taking up most of the window and 'next', 'back' and 'cancel' buttons at the bottom; by clicking the 'next' button, the current window is closed and a new one opens which looks the same, except that it has different options (or perhaps it is the same window but its contents have been destroyed, I'm not sure which would be better). The rough layout I want is shown in this image
I have looked around for example codes which do something similar to this but have not been able to find any. I have seen this answer, but it's not quite what I want. I have used this tutorial to learn what I know about tkinter but I can't find an answer in it.
Here is my extremely poor attempt at a simplified version of what I want to do: When I run the code it creates a window with two buttons. The 'Quit' button works fine; however, when I click the 'Next' button it closes the window and opens a new one as desired but it also opens another window.
from tkinter import * from tkinter import ttk def win1(): mainframe = ttk.Frame(root, padding = '3 3 12 12') mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S)) mainframe.columnconfigure(0, weight = 1) mainframe.rowconfigure(0, weight = 1) ttk.Button(mainframe, text = 'Next', command = win2).grid( column = 1, row = 1, sticky = W) ttk.Button(mainframe, text = 'Quit', command = quit).grid( column = 1, row = 2, sticky = W) root.mainloop() def quit(): root.destroy() def win2(): quit() new = Toplevel() new.title('Window 2') new = ttk.Frame(root, padding = '3 3 12 12') new.grid(column = 0, row = 0, sticky = (N, W, E, S)) new.columnconfigure(0, weight = 1) new.rowconfigure(0, weight = 1) ttk.Button(mainframe, text = 'Next', command = win2).grid( column = 1, row = 1, sticky = W) root = Tk() win1()
This gives the following error message (which I don't understand):
Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.3/tkinter/__init__.py", line 1478, in __call__ return self.func(*args) File "<stdin>", line 23, in win2 File "/usr/lib/python3.3/tkinter/ttk.py", line 733, in __init__ Widget.__init__(self, master, "ttk::frame", kw) File "/usr/lib/python3.3/tkinter/ttk.py", line 553, in __init__ tkinter.Widget.__init__(self, master, widgetname, kw=kw) File "/usr/lib/python3.3/tkinter/__init__.py", line 2078, in __init__ (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: this isn't a Tk applicationNULL main window
Apart from the fact that it doesn't actually do what I want, I feel like I am going about this the completely wrong way (defining the windows in a function, etc), and will run into a lot of trouble when I want to make it more complicated. Would anyone be able to rewrite my code in a better way and in a way that helps me build to more complicated programs, offer a resource for learning what I need to make the program I want or even offer advice? Thanks.
原文:https://stackoverflow.com/questions/24161936
最满意答案
永远不要将您的微调器适配器设置为null!
mySpinner.setAdapter(null);
我这样做是因为我在应用程序的功能中需要它。 所以我只是用
mySpinner.setEnabled(false)
来妥协。Never set your spinner adapter to null!
mySpinner.setAdapter(null);
I did that because I needed it in my application's feature. So I just used
mySpinner.setEnabled(false)
to compromise.
相关问答
更多-
NullPointerException onCreate - android spinner(NullPointerException onCreate - android spinner)[2022-03-11]
您尚未为btnJoin分配任何值。 这导致异常。 编辑1 - 如何解决问题 要解决此问题,以及单击按钮时可能发生的其他问题,请在setContentView(R.layout.activity_members_register)之后setContentView(R.layout.activity_members_register)添加到onCreate()方法: txtName = (EditText)findViewById(R.id.register_member_name); txtPassword ... -
找到了! setRequestedOrientation工作正常,但home.xml不是从layout-land中获取的,因为从Android 3.2开始,有一种支持多个屏幕的新方法。 然后,您应该使用sw * N * dp,如: DeclaringTabletLayouts中所述 所以我在res:layout-sw600dp中创建了一个新文件夹,并将横向home.xml放在该文件夹中,这就是解决方案。 Found it! The setRequestedOrientation worked fine, b ...
-
Spinner和ArrayAdapter Android中的NullPointerException(Spinner and ArrayAdapter NullPointerException in Android)[2022-12-11]
你的微调无效。 看,首先你创建ViewPager并使setContentView : setContentView(viewPager); 但你的ViewPager是空的,因为你没有给它任何东西。 接下来你打电话给: spinner = (Spinner) findViewById(R.id.tabsSpinner); 这意味着findViewById将查找您的微调器,从您之前创建的Activity - ViewPager根视图开始,这是空的。 所以Spinner永远不会被发现 - 并且它始终为空。 Y ... -
永远不要将您的微调器适配器设置为null! mySpinner.setAdapter(null); 我这样做是因为我在应用程序的功能中需要它。 所以我只是用mySpinner.setEnabled(false)来妥协。 Never set your spinner adapter to null! mySpinner.setAdapter(null); I did that because I needed it in my application's feature. So I just used m ...
-
Android OnClickListener抛出NullPointerException(Android OnClickListener throws NullPointerException)[2022-03-31]
您的代码中的一个问题。 没有设置Button的id b.setText("0"); b.setOnClickListener(buttonListener); b.setId(i); 我希望它对你有所帮助。 One problem in your code. Have not set id of Button b.setText("0"); b.setOnClickListener(buttonListener); ... -
您可以输入默认选择文本,例如“选择一个”或默认值。 或者您可以按如下方式处理Null值检查: String selected_text = "none" if (hubungan_pp.getSelectedItem() != null) { selected_text = hubungan_pp.getSelectedItem().toString(); } 希望这可以帮助。 You can either put a default selection text like 'Select One ...
-
Dialog中的微调器 - NullPointerException - Android(Spinner in Dialog - NullPointerException - Android)[2022-03-19]
尝试这个: Spinner spinner = (Spinner)layout.findViewById(R.id.spinner1); Try this: Spinner spinner = (Spinner)layout.findViewById(R.id.spinner1); -
你的Spinner器可能还没有被充气。 如果您想操纵视图,请自己膨胀View ,然后在充气View上使用setContentView 。 查看关于创建对话框的文档 。 更新: 在新代码中,更改: spin = (Spinner) findViewById(R.id.spinQ); 至: spin = (Spinner) layout.findViewById(R.id.spinQ); Your Spinner is probably not inflated yet. If you want to ma ...
-
原因:你点击了button1但是它还没有被初始化(init。在FloatingActionButton里面) 解决方案尝试此代码: Button button1; TextView txtspr; EditText urlInputEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layo ...
-
这是你的oncreate()方法中的问题: Spinner spinner = (Spinner) findViewById(R.id.widget28); 您将结果分配给spinner ,这是一个局部变量/参考。 main.spinner类字段仍为null 。 这就是为什么你在calculate()方法中有NullPointerException 。 将上面的行替换为: this.spinner = (Spinner) findViewById(R.id.widget28); Here is the ...