gpt4 book ai didi

python - 有关 Python TKinter 动态选项菜单的更多信息

转载 作者:行者123 更新时间:2023-11-30 23:20:54 24 4
gpt4 key购买 nike

我正在尝试修改 code here让用户确认从选项菜单中选择的项目。如果用户单击“提交”按钮,则会打开一个消息框提示确认。最后,我希望所选项目作为变量返回到程序,以便它们可以在其他函数中使用以进行进一步处理。然而我的修改并没有起作用;它只是返回一个空窗口。想想我缺少什么?非常感谢。

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' object has no attribute 'optionmenu_b'

最佳答案

在这里@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()

希望这对您有帮助。

关于python - 有关 Python TKinter 动态选项菜单的更多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25194969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com