有关Python TKinter Dynamic OptionMenu的更多信息

时间:2014-08-08 01:53:58

标签: python user-interface tkinter optionmenu

我尝试修改code here以让用户确认从optionmenus中选择的项目。如果用户单击“提交”按钮,则会打开一个消息框提示确认。最后,我希望所选项目作为变量返回到程序中,以便它们可以在其他函数中用于进一步处理。但是,我的修改不起作用;它只返回一个空窗口。关于我失踪的想法?非常感谢。

from tkinter import *
import tkinter.messagebox

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.dict = {'Asia': ['Japan', 'China', 'Malasia'],
                     'Europe': ['Germany', 'France', 'Switzerland'],
                     'Africa': ['Nigeria', 'Kenya', 'Ethiopia']}
        self.variable_a = StringVar(self)
        self.variable_b = StringVar(self)
        self.variable_a.trace('w', self.updateoptions)
        self.optionmenu_a = OptionMenu(self, self.variable_a, *self.dict.keys())
        self.variable_a.set('Asia')
        self.optionmenu_a.pack()
        self.optionmenu_b = OptionMenu(self, self.variable_b, ())
        self.optionmenu_b.pack()
        self.btn = Button(self, text="Submit", width=8, command=self.submit)
        self.btn.pack()
        self.pack()

    def updateoptions(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 country=country: self.variable_b.set(country))

    def submit(self, *args):
        var1 = self.variable_a.get()
        var2 = self.variable_b.get()
        if tkinter.messagebox.askokcancel("Selection", "Confirm selection: " + var1 + ' ' + var2):
            print(var1, var2) #Or can be other function for further processing


root = Tk()
app = App(root)
app.mainloop()

Python版本3.4.1

编辑:窗口现在显示小部件。我在按钮前省略了self.。我仍然收到一条错误消息,我正在尝试解决这个问题:AttributeError:' App'对象没有属性' optionmenu_b'

1 个答案:

答案 0 :(得分:3)

在这里@sedeh,这可以按你的意愿运作。该错误并非来自您的添加内容,但我认为使用from tkinter import *代替import tkinter as tk,这就是为什么在运行代码时,一旦tk窗口出现就会出现错误。

我所做的是从您提供的链接中获取代码,添加了您所做的并且没有错误。

import tkinter as tk
import tkinter.messagebox

class App(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.dict = {'Asia': ['Japan', 'China', 'Malasia'],
                     'Europe': ['Germany', 'France', 'Switzerland'],
                     'Africa': ['Nigeria', 'Kenya', 'Ethiopia']}
        self.variable_a = tk.StringVar(self)
        self.variable_b = tk.StringVar(self)
        self.variable_a.trace('w', self.updateoptions)
        self.optionmenu_a = tk.OptionMenu(self, self.variable_a, *self.dict.keys())
        self.optionmenu_b = tk.OptionMenu(self, self.variable_b, '')
        self.variable_a.set('Asia')
        self.optionmenu_a.pack()
        self.optionmenu_b.pack()
        self.btn = tk.Button(self, text="Submit", width=8, command=self.submit)
        self.btn.pack()
        self.pack()
    def updateoptions(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 country=country: self.variable_b.set(country))

    def submit(self, *args):
        var1 = self.variable_a.get()
        var2 = self.variable_b.get()
        if tkinter.messagebox.askokcancel("Selection", "Confirm selection: " + var1 + ' ' + var2):
            print(var1, var2) #Or can be other function for further processing

root = tk.Tk()
app = App(root)
app.mainloop()

我希望这会对你有所帮助。