Python - tkinter - 如何在命令参数中调用方法
Python - tkinter - How to call a method in command argument
我正在尝试编写以下代码,但它没有按预期工作
问题:
我想首先在 tkinter window 中显示登录按钮,然后弹出一个输入凭据,
但是当我尝试调用命令参数中的方法时,首先出现 "Enter Credentials" 弹出窗口,然后出现 "Login" 按钮,但该按钮没有任何效果。
请建议在这种情况下如何调用登录按钮。
代码如下:
import sys
import tkinter.messagebox as box
from tkinter.filedialog import asksaveasfile
if sys.version_info[0] >= 3:
import tkinter as tk
else:
import Tkinter as tk
class App(tk.Frame):
def __init__(self, master, username, password):
tk.Frame.__init__(self, master)
self.dict = {'Asia': ['Japan', 'China', 'Malaysia'],
'Europe': ['Germany', 'France', 'Switzerland']}
self.variable_a = tk.StringVar(self)
self.variable_b = tk.StringVar(self)
self.variable_a.trace('w', self.update_options)
self.variable_b.trace('w', self.fun2)
self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict.keys(), command=self.fun)
self.optionmenu_b = tk.OptionMenu(self, self.variable_b, '')
export = self.sample(username, password)
self.button = tk.Button(self,text="Login", command=export)
self.variable_a.set('Asia')
self.optionmenu_a.pack()
self.optionmenu_b.pack()
self.button.pack()
self.pack()
def fun(self,value):
print(value)
def fun2(self, *args):
print(self.variable_b.get())
def sample(self, username, password):
box.showinfo('info', 'Enter Credentials')
def update_options(self, *args):
countries = self.dict[self.variable_a.get()]
self.variable_b.set(countries[0])
menu = self.optionmenu_b['menu']
menu.delete(0, 'end')
for country in countries:
menu.add_command(label=country, command=lambda nation=country: self.variable_b.set(nation))
if __name__ == "__main__":
root = tk.Tk()
username = "root"
password = "admin"
app = App(root, username, password)
app.mainloop()
问题出在你如何声明函数 export
来显示弹出窗口,它调用了显示消息框的 sample
方法,这就是你也得到弹出窗口的原因很快。
作为快速修复,直接在命令中用 lambda 替换 export
东西:
self.button = tk.Button(self,
text="Login",
command=lambda : self.sample(username, password))
我正在尝试编写以下代码,但它没有按预期工作
问题: 我想首先在 tkinter window 中显示登录按钮,然后弹出一个输入凭据,
但是当我尝试调用命令参数中的方法时,首先出现 "Enter Credentials" 弹出窗口,然后出现 "Login" 按钮,但该按钮没有任何效果。
请建议在这种情况下如何调用登录按钮。
代码如下:
import sys
import tkinter.messagebox as box
from tkinter.filedialog import asksaveasfile
if sys.version_info[0] >= 3:
import tkinter as tk
else:
import Tkinter as tk
class App(tk.Frame):
def __init__(self, master, username, password):
tk.Frame.__init__(self, master)
self.dict = {'Asia': ['Japan', 'China', 'Malaysia'],
'Europe': ['Germany', 'France', 'Switzerland']}
self.variable_a = tk.StringVar(self)
self.variable_b = tk.StringVar(self)
self.variable_a.trace('w', self.update_options)
self.variable_b.trace('w', self.fun2)
self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict.keys(), command=self.fun)
self.optionmenu_b = tk.OptionMenu(self, self.variable_b, '')
export = self.sample(username, password)
self.button = tk.Button(self,text="Login", command=export)
self.variable_a.set('Asia')
self.optionmenu_a.pack()
self.optionmenu_b.pack()
self.button.pack()
self.pack()
def fun(self,value):
print(value)
def fun2(self, *args):
print(self.variable_b.get())
def sample(self, username, password):
box.showinfo('info', 'Enter Credentials')
def update_options(self, *args):
countries = self.dict[self.variable_a.get()]
self.variable_b.set(countries[0])
menu = self.optionmenu_b['menu']
menu.delete(0, 'end')
for country in countries:
menu.add_command(label=country, command=lambda nation=country: self.variable_b.set(nation))
if __name__ == "__main__":
root = tk.Tk()
username = "root"
password = "admin"
app = App(root, username, password)
app.mainloop()
问题出在你如何声明函数 export
来显示弹出窗口,它调用了显示消息框的 sample
方法,这就是你也得到弹出窗口的原因很快。
作为快速修复,直接在命令中用 lambda 替换 export
东西:
self.button = tk.Button(self,
text="Login",
command=lambda : self.sample(username, password))