Tkinter ttk 组合框默认值
Tkinter ttk Combobox Default Value
我正在构建一个 Tkinter 应用程序,我遇到了为组合框设置默认值的问题。我设法解决了这个问题,但我很想知道它为什么起作用,我想知道是否有更好的方法。
我有一个 tk.Toplevel()
window 使用 fowling 代码弹出一个组合框:
class add_equation():
def __init__(self):
self.add_window = tk.Toplevel()
self.add_window.title("Add Equation Set")
self.add_window.resizable(width=False, height=False)
self.name_entry_var = tk.StringVar()
self.name_entry = ttk.Entry(self.add_window, textvariable=self.name_entry_var, width=30)
self.name_entry.grid(row=1, columnspan=2, stick="w")
self.equation_type_var = tk.StringVar()
self.equation_type = ttk.Combobox(self.add_window, textvariable=self.equation_type_var, values=("System", "Exact", "Import Point List..."), state="readonly", width=28, postcommand =lambda: self.add_window.update())
self.equation_type.current(0)
self.equation_type.grid(row=2, columnspan=2, sticky="w")
self.add_window.update()
class add_quation()
在以下代码中调用:
import tkinter as tk
from tkinter import ttk
class Solver_App(tk.Tk, ttk.Frame):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
list_frame = ttk.Frame(self, height=50)
list_frame.pack(side="top", fill="y", expand=True, anchor="w")
# Button that will call the class add_equation that creates a new window.
add_button = ttk.Button(list_frame, text="Add Equation Set", command=add_equation)
add_button.pack(side="top", expand=False, fill="x", padx=10, pady=5, anchor="n")
app = Solver_App()
app.mainloop()
回顾add_equation()
class,如果你删除self.equation_type.current(0)
或postcommand =lambda: self.add_window.update()
,默认值将不再显示,但两者都有效美好的。为什么它会这样工作而不是只有 self.equation_type.current(0)
?
我试图找到一种更优雅的方法来执行此操作,并且我发现了一些相关的东西 over here,但是我没有运气实现该方法,我假设从按钮调用 add_equation()
命令可能与此有关。
*我在 Mac OS X Yosemite.
上使用 Python 3.4
我认为这可能是因为您正在通过调用 add_equation
构造函数创建 window,并且 window 会立即被垃圾收集(或至少 python 句柄是)所以永远不会正确刷新。
我会把它重写成这样:
class Equation_Window(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
self.title("Add Equation Set")
self.resizable(width=False, height=False)
self.name_entry_var = tk.StringVar()
self.name_entry = ttk.Entry(self, textvariable=self.name_entry_var, width=30)
self.name_entry.grid(row=1, columnspan=2, stick="w")
self.equation_type_var = tk.StringVar()
self.equation_type = ttk.Combobox(self, textvariable=self.equation_type_var, values=("System", "Exact", "Import Point List..."), state="readonly", width=28)
self.equation_type.current(0)
self.equation_type.grid(row=2, columnspan=2, sticky="w")
def add_equation():
w = Equation_Window()
w.wait_window()
(其他一切保持不变)
我已将您的 add_equation
class 更改为派生自 tk.Toplevel
的内容(并重命名),我认为这更有意义。然后,我创建了 add_equation
一个函数,并调用了 wait_window
(其作用类似于 mainloop
,但仅针对一个 window)。 wait_window
将保持 w
活动,直到 window 关闭,因此所有内容都会正确刷新。
我正在构建一个 Tkinter 应用程序,我遇到了为组合框设置默认值的问题。我设法解决了这个问题,但我很想知道它为什么起作用,我想知道是否有更好的方法。
我有一个 tk.Toplevel()
window 使用 fowling 代码弹出一个组合框:
class add_equation():
def __init__(self):
self.add_window = tk.Toplevel()
self.add_window.title("Add Equation Set")
self.add_window.resizable(width=False, height=False)
self.name_entry_var = tk.StringVar()
self.name_entry = ttk.Entry(self.add_window, textvariable=self.name_entry_var, width=30)
self.name_entry.grid(row=1, columnspan=2, stick="w")
self.equation_type_var = tk.StringVar()
self.equation_type = ttk.Combobox(self.add_window, textvariable=self.equation_type_var, values=("System", "Exact", "Import Point List..."), state="readonly", width=28, postcommand =lambda: self.add_window.update())
self.equation_type.current(0)
self.equation_type.grid(row=2, columnspan=2, sticky="w")
self.add_window.update()
class add_quation()
在以下代码中调用:
import tkinter as tk
from tkinter import ttk
class Solver_App(tk.Tk, ttk.Frame):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
list_frame = ttk.Frame(self, height=50)
list_frame.pack(side="top", fill="y", expand=True, anchor="w")
# Button that will call the class add_equation that creates a new window.
add_button = ttk.Button(list_frame, text="Add Equation Set", command=add_equation)
add_button.pack(side="top", expand=False, fill="x", padx=10, pady=5, anchor="n")
app = Solver_App()
app.mainloop()
回顾add_equation()
class,如果你删除self.equation_type.current(0)
或postcommand =lambda: self.add_window.update()
,默认值将不再显示,但两者都有效美好的。为什么它会这样工作而不是只有 self.equation_type.current(0)
?
我试图找到一种更优雅的方法来执行此操作,并且我发现了一些相关的东西 over here,但是我没有运气实现该方法,我假设从按钮调用 add_equation()
命令可能与此有关。
*我在 Mac OS X Yosemite.
上使用 Python 3.4我认为这可能是因为您正在通过调用 add_equation
构造函数创建 window,并且 window 会立即被垃圾收集(或至少 python 句柄是)所以永远不会正确刷新。
我会把它重写成这样:
class Equation_Window(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
self.title("Add Equation Set")
self.resizable(width=False, height=False)
self.name_entry_var = tk.StringVar()
self.name_entry = ttk.Entry(self, textvariable=self.name_entry_var, width=30)
self.name_entry.grid(row=1, columnspan=2, stick="w")
self.equation_type_var = tk.StringVar()
self.equation_type = ttk.Combobox(self, textvariable=self.equation_type_var, values=("System", "Exact", "Import Point List..."), state="readonly", width=28)
self.equation_type.current(0)
self.equation_type.grid(row=2, columnspan=2, sticky="w")
def add_equation():
w = Equation_Window()
w.wait_window()
(其他一切保持不变)
我已将您的 add_equation
class 更改为派生自 tk.Toplevel
的内容(并重命名),我认为这更有意义。然后,我创建了 add_equation
一个函数,并调用了 wait_window
(其作用类似于 mainloop
,但仅针对一个 window)。 wait_window
将保持 w
活动,直到 window 关闭,因此所有内容都会正确刷新。