英文:
Not able to run this code. After two levels, the code seems to fail, in retrieving the values required to run the next level
问题
这是您提供的Python代码的翻译:
import tkinter as tk
class App:
def __init__(self, master):
self.master = master
self.master.geometry("300x200")
self.master.title("文档查找器")
# 创建下拉菜单
self.location_var = tk.StringVar()
self.location_var.set("选择地块")
self.location_dropdown = tk.OptionMenu(self.master, self.location_var, "地块A", "地块B", command=self.update_sub_area_menu)
self.location_dropdown.pack()
self.sub_area_var = tk.StringVar()
self.sub_area_var.set("选择子区域")
self.sub_area_dropdown = tk.OptionMenu(self.master, self.sub_area_var, "", command=self.update_level_menu)
self.sub_area_dropdown.pack()
self.level_var = tk.StringVar()
self.level_var.set("选择楼层")
self.level_dropdown = tk.OptionMenu(self.master, self.level_var, "", command=self.update_room_menu)
self.level_dropdown.pack()
self.room_var = tk.StringVar()
self.room_var.set("选择房间")
self.room_dropdown = tk.OptionMenu(self.master, self.room_var, "")
self.room_dropdown.pack()
# 创建搜索文档的按钮
self.search_button = tk.Button(self.master, text="提交")
self.search_button.pack()
def update_sub_area_menu(self, selection):
if selection == "地块A":
sub_areas = ["地下室A", "俱乐部休息室", "酒店-豪华", "酒店-服务公寓", "酒店-超豪华", "连接层", "其他", "阁楼", "基座A", "租赁办公室", "餐厅", "水疗/健身房"]
elif selection == "地块B":
sub_areas = ["公寓-复式", "公寓-单层", "地下室B", "连接层", "机械与屋顶", "基座B"]
else:
sub_areas = []
self.sub_area_var.set("选择子区域")
self.sub_area_dropdown['menu'].delete(0, 'end')
for sub_area in sub_areas:
self.sub_area_dropdown['menu'].add_command(label=sub_area, command=tk._setit(self.sub_area_var, sub_area))
def update_level_menu(self, selection):
if selection == "地下室A":
levels = ["B1", "B2", "B3", "B4", "B5", "B6", "B7"]
elif selection == "俱乐部休息室":
levels = ["53F"]
# 其他子区域的楼层设置...
else:
levels = []
self.level_dropdown['menu'].delete(0, 'end')
for level in levels:
self.level_dropdown['menu'].add_command(label=level, command=tk._setit(self.level_var, level))
def update_room_menu(self, selection):
if selection == "B1":
rooms = ["FWT BA 001", "FWT BA 002", "RAMP 1 BA 003", "MECH WATER TANK (WATER FEATURE 13) BA 004"]
else:
rooms = []
self.room_var.set("选择房间")
self.room_dropdown['menu'].delete(0, 'end')
for room in rooms:
self.room_dropdown['menu'].add_command(label=room, command=tk._setit(self.room_var, room))
root = tk.Tk()
app = App(root)
root.mainloop()
这个代码使用Python 3编写,是一个简单的GUI应用,用于选择不同的地块、子区域、楼层和房间。用户可以通过下拉菜单选择不同的选项。如果在选择地块和子区域之后出现问题,代码似乎没有为楼层提供选择标准。您可以查看update_level_menu
函数,以确保楼层选择与所选地块和子区域相匹配。
英文:
I have made a GUI for selecting different floors. The user has to mention the plot, after which the output is shown for the levels/clusters and then in the next level, an option is given to select the level, and in the next, the option to select the room. But after finishing two level, i.e., after selecting the plot and the cluster, the code seems to fail, it is not giving the selection criteria for the floor.
Here's my code:
import tkinter as tk
class App:
def __init__(self, master):
self.master = master
self.master.geometry("300x200")
self.master.title("Document Finder")
# Create dropdown menus
self.location_var = tk.StringVar()
self.location_var.set("Select the Plot")
self.location_dropdown = tk.OptionMenu(self.master, self.location_var, "Plot A", "Plot B",command = self.update_sub_area_menu)
self.location_dropdown.pack()
self.sub_area_var = tk.StringVar()
self.sub_area_var.set("Select the sub-area")
self.sub_area_dropdown = tk.OptionMenu(self.master, self.sub_area_var,"",command = self.update_level_menu)
self.sub_area_dropdown.pack()
self.level_var = tk.StringVar()
self.level_var.set("Select a level")
self.level_dropdown = tk.OptionMenu(self.master, self.level_var,"",command=self.update_room_menu)
self.level_dropdown.pack()
self.room_var = tk.StringVar()
self.room_var.set("Select a room")
self.room_dropdown = tk.OptionMenu(self.master, self.room_var,"")
self.room_dropdown.pack()
# Create button to search for document
self.search_button = tk.Button(self.master, text="Submit")
self.search_button.pack()
def update_sub_area_menu(self,selection):
if selection == "Plot A":
sub_areas = ["Basement A", "Club Lounge", "Hotel-Luxury", "Hotel-Service Apartments", "Hotel-Ultra Luxury", "Link Levels", "Others", "Penthouses", "Podium A", "Rental Offices", "Restaurants", "Spa/Gym"]
elif selection == "Plot B":
sub_areas = ["Apartments-Duplex", "Apartments-Simplex", "Basement B", "Link", "Mech & Roof", "Podium B"]
else:
sub_areas = []
self.sub_area_var.set("Select the sub-area")
self.sub_area_dropdown['menu'].delete(0, 'end')
for sub_area in sub_areas:
self.sub_area_dropdown['menu'].add_command(label=sub_area, command=tk._setit(self.sub_area_var, sub_area))
def update_level_menu(self, selection):
if selection == "Basement A":
levels = ["B1", "B2", "B3", "B4", "B5", "B6", "B7"]
elif selection == "Club Lounge":
levels = ["53F"]
elif selection == "Hotel-Luxury":
levels = ["30F", "31F", "32F", "33F", "34F", "35F", "36F", "37F", "38F", "39F", "40F", "41F", "42F", "43F", "44F"]
elif selection == "Hotel-Service Apartments":
levels = ["54F", "55F", "56F", "57F", "58F", "59F", "60F"]
elif selection == "Hotel-Ultra Luxury":
levels = ["46F", "47F", "48F", "49F","50F", "51F", "52"]
elif selection == "Link Levels":
levels = ["23F", "24F", "25F", "26F"]
elif selection == "Others":
levels = ["22F", "23F", "29F", "45F", "67F", "RF", "RF"]
elif selection == "Penthouses":
levels = ["61F", "62F", "63F", "64F", "65F", "66F"]
elif selection == "Podium A":
levels = ['GF', "1F", "2F","3F"]
elif selection == "Rental Offices":
levels = ["5F", "6F", "7F", "8F", "9F", "10F", "11F", "12F", "13F", "14F", "15F","16F","17F","18F","19F","20F","21F"]
elif selection == "Restaurants":
levels = ["4F"]
elif selection == "Spa/Gym":
levels = ["27F", "28F"]
elif selection == "Apartments-Duplex":
levels = ["30F", "31F", "32F", "33F", "34F", "35F", "36F", "37F", "38F", "39F", "40F", "41F", "42F", "43F", "44F", "45", "46F", "47F", "48F", "49F","50F", "51F", "52F", "53F", "54F","55F", "56F","57F","58F"]
elif selection == "Apartments-Simplex":
levels = ["5F", "6F", "7F", "8F", "9F","10F","11F","12F","13F","14F","15F","16F","17F","18F","19F","20F","21F","22F","23F","24F","25F","26F"]
elif selection == "Basement B":
levels = ["B1","B2","B3","B4","B5","B6","B7"]
elif selection == "Link":
levels = ["28F", "29F"]
elif selection == "Mech & Roof":
levels = ["RF"]
elif selection == "Podium B":
levels = ["GF","1F","2F","4F"]
else:
levels = []
self.level_dropdown['menu'].delete(0, 'end')
for level in levels:
self.level_dropdown['menu'].add_command(label=level, command=tk._setit(self.level_var, level))
def update_room_menu(self, selection):
if selection == "B1":
rooms = ["FWT BA 001","FWT BA 002","RAMP 1 BA 003","MECH WATER TANK (WATER FEATURE 13) BA 004",
]
else:
rooms = []
self.room_var.set("Select the room")
self.room_dropdown['menu'].delete(0, 'end')
for room in rooms:
self.room_dropdown['menu'].add_command(label=room, command=tk._setit(self.room_var, room))
root = tk.Tk()
app = App(root)
root.mainloop()
I'm using Python 3
.
答案1
得分: 1
你忘记将所需的函数传递给_setit(...)
:
...
class App:
def __init__(self, master):
...
def update_sub_area_menu(self, selection):
...
for sub_area in sub_areas:
self.sub_area_dropdown['menu'].add_command(label=sub_area,
command=tk._setit(self.sub_area_var, sub_area, self.update_level_menu))
def update_level_menu(self, selection):
...
for level in levels:
self.level_dropdown['menu'].add_command(label=level,
command=tk._setit(self.level_var, level, self.update_room_menu))
...
英文:
You forget to pass the required function to _setit(...)
:
...
class App:
def __init__(self, master):
...
def update_sub_area_menu(self, selection):
...
for sub_area in sub_areas:
self.sub_area_dropdown['menu'].add_command(label=sub_area,
command=tk._setit(self.sub_area_var, sub_area, self.update_level_menu))
def update_level_menu(self, selection):
...
for level in levels:
self.level_dropdown['menu'].add_command(label=level,
command=tk._setit(self.level_var, level, self.update_room_menu))
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论