gpt4 book ai didi

python 入口小部件元组组合

转载 作者:行者123 更新时间:2023-12-01 06:04:24 25 4
gpt4 key购买 nike

我一直在尝试将条目小部件与 python 2.7 tkinter 中的元组结合起来。使用此代码,我应该能够输入水果的名称,在水果碗元组中搜索它,如果它在其中,则使用一组单选按钮显示该水果的特征。

from Tkinter import*

class Fruit:
def __init__(self, parent):

# variables
self.texture_option = StringVar()
self.climate_option = StringVar()

# layout
self.myParent = parent

self.main_frame = Frame(parent, background="light blue")
self.main_frame.pack(expand=YES, fill=BOTH)

texture_options = ["Soft", "Crunchy","?"]
climate_options = ["Temperate", "Tropical","?"]

self.texture_option.set("?")
self.climate_option.set("?")

self.texture_options_frame = Frame(self.main_frame, borderwidth=3, background="light blue")
self.texture_options_frame.pack(side=TOP, expand=YES, anchor=W)
Label(self.texture_options_frame, text="Texture:", relief=FLAT, font="bold", background="light blue").pack(side=LEFT,anchor=W)
for option in texture_options:
button = Radiobutton(self.texture_options_frame, text=str(option), indicatoron=0,
value=option, padx=5, variable=self.texture_option, background="light blue")
button.pack(side=LEFT)

self.climate_options_frame = Frame(self.main_frame, borderwidth=3, background="light blue")
self.climate_options_frame.pack(side=TOP, expand=YES, anchor=W)
Label(self.climate_options_frame, text="Climate:", relief=FLAT, font="bold", background="light blue").pack(side=LEFT,anchor=W)
for option in climate_options:
button = Radiobutton(self.climate_options_frame, text=str(option), indicatoron=0,
value=option, padx=5, variable=self.climate_option, background="light blue")
button.pack(side=LEFT)

#search button
self.search_frame = Frame(self.main_frame, borderwidth=5, height=50, background="light blue")
self.search_frame.pack(expand=NO)

enter = Entry(self.search_frame, width=30).pack(side=LEFT, expand=NO, padx=5, pady=5, ipadx=5, ipady=5)

self.searchbutton = Button(self.search_frame, text="Search", foreground="white", background="blue",
width=6, padx="2m", pady="1m")
self.searchbutton.pack(side=LEFT, pady=5)
self.searchbutton.bind("<Button-1>", self.searchbuttonclick)
self.searchbutton.bind("<Return>", self.searchbuttonclick)


def searchbuttonclick(self,event):
#fruit texture climate
fruit_bowl=[
('Apple', 'Crunchy','Temperate'),
('Orange', 'Soft','Tropical'),
('Pawpaw','Soft','Temperate')]

if enter.get()==fruit_bowl[x][0]:
self.texture_option.set()==fruit_bowl[x][1]
self.climate_option.set()==fruit_bowl[x][2]

root = Tk()
root.title("Fruit Bowl")
fruit = Fruit(root)
root.mainloop()

我收到一条错误消息,名称错误:未定义全局名称“enter”。有谁知道为什么这不起作用?

最佳答案

问题是你在对象的一个​​方法中本地定义了“enter”变量,然后尝试在另一种方法中访问它 - 它不作为局部变量存在(因此Python将其作为全局变量搜索,并且由于它没有找到它,它会引发您看到的消息错误)。

很容易修复:只需将 Enter 变量设置为“Fruit”类的属性,在使用它的任何地方都使用 self. 前缀 - 这样它将可供所有人使用你的对象的方法 -

即,更改此行:

enter = Entry(self.search_frame, width=30).pack(side=LEFT, Expand=NO, padx=5, pady=5, ipadx=5, ipady=5)

至:

self.enter = Entry(self.search_frame, width=30).pack(side=LEFT, Expand=NO, padx=5, pady=5, ipadx=5, ipady=5)

if Enter.get()==fruit_bowl[x][0]:if self.enter.get()==fruit_bowl[x][0]:

关于python 入口小部件元组组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8875195/

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