从一个列表中创建多个组合框并接收每个组合框的结果?

2024-04-26 06:21:39 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的第一篇文章,所以我不完全确定这是如何工作的。该程序的最终目标是使用Python Tkinter创建GUI,以规划密歇根州的公路旅行。代码中棘手的部分是有多个框架来问多个问题。我的第一个是他们可以选择访问的城市的检查按钮列表;然后,他们选择的城市被绘制在画布上,并存储在选定城市的列表中。然后,continue_1按钮将它们带到第二个帧,我目前遇到了问题

我想从所选城市列表中创建组合框,从城市1开始,他们可以在其中选择旅行顺序。但是,我很难存储每个选择,特别是因为我使用了for循环。目前,我的程序为每个选择返回Py_Var。如何使其返回实际值,而不是其他值?我不知道是使用.get()还是.append()或其他什么。此外,我是否必须像我所做的那样,将组合框选择绑定到命令以添加值

另外,我知道其他地方也有人问过这个问题,但可能不是在这个背景下。是否有一种方法可以在选择城市后从其他组合框中删除城市。你能给我的任何帮助都会很好!这是一场斗争

from tkinter import *
from tkinter import ttk

class MichiganRoadTrip:
    '''MichiganRoadTrip runs a TKinter program modeling a road trip planner and display screen based off user input.'''

    def __init__(self, window):
        self.window = window
        self.window.geometry('950x750')
        picture = PhotoImage(file='michigan_map.gif')
        self.window.picture = picture

        self.canvas = Canvas(self.window, bg='white', width=500, height=550, highlightbackground='navy')
        self.canvas.grid(row=0, column=0)
        self.canvas.create_image(250, 275, image=picture)

        self.canvas.bind('<Button-1>', self.display_coordinates)

        self.make_frame_1()

        results_frame = Frame(window)
        results_frame.grid(row=1, sticky=W)

    def make_frame_1(self):
        self.frame_1 = Frame(self.window)
        self.frame_1.grid(row=0, column=1, sticky=N)
        welcome_label = Label(self.frame_1, text='Hello! Welcome to the Road Trip Planner!')
        welcome_label.grid()

        self.cities = {'Holland': [257, 445], 'Grand Rapids': [281, 427], 'Ann Arbor': [396, 482],
                    'Traverse City': [291, 292], 'Lansing': [349, 452], 'Kalamazoo': [293, 486],
                    'Detroit': [430, 477],'Frankenmuth': [394, 401], 'Sault Ste. Marie': [357, 154],
                    'Sleeping Bear Dunes National Park': [265, 280]}
        self.selected = {}
        for city in self.cities:
            is_selected = BooleanVar()
            self.city_button = Checkbutton(self.frame_1, text=city, variable=is_selected)
            self.city_button.grid(sticky=W)
            self.selected[city] = is_selected

        continue1_button = Button(self.frame_1, text='Continue', command=lambda:[self.choose_cities(), self.make_frame_2(),
                                                                                 self.frame_1.destroy()])
        continue1_button.grid(sticky=N)

    def make_frame_2(self):
        self.frame_2 = Frame(self.window)
        self.frame_2.grid(row=0, column=1, sticky=N)
        self.frame_2_label = Label(self.frame_2, text='Next choose the order\nin which you would like to go.')
        self.frame_2_label.grid()

        self.chosen_city = {}
        self.city_combobox = {}
        self.ordered_city_list = []
        for city in self.selected_cities:
            self.city_label = Label(self.frame_2, text='City #' + str((self.selected_cities.index(city) + 1)) + ': ')
            self.city_label.grid(sticky=W)
            self.chosen_city[city] = BooleanVar()
            self.city_combobox[city] = ttk.Combobox(self.frame_2, values=self.selected_cities,
                                                    textvariable=self.chosen_city[city], width=25, state='readonly')
            self.city_combobox[city].grid(sticky=W)
            self.city_combobox[city].bind('<<ComboboxSelected>>', self.ordered_city_list.append(self.city_combobox[city].get()))

        continue2_button = Button(self.frame_2, text='Next', command=self.frame_2.destroy)
        continue2_button.grid(sticky=N)      

    def choose_cities(self):
        self.selected_cities = []
        for city in self.cities:
            if self.selected[city].get():
                self.selected_cities.append(city)
                self.canvas.create_oval((self.cities[city][0] - 8), (self.cities[city][1] - 8),
                                        (self.cities[city][0] + 8), (self.cities[city][1] + 8), fill='orange')
                if city == 'Traverse City' or city == 'Detroit':
                    self.canvas.create_text((self.cities[city][0] + 3), (self.cities[city][1] + 3), text=city, anchor=NW, fill='orange')
                else:
                    self.canvas.create_text((self.cities[city][0] - 3), (self.cities[city][1] - 3), text=city, anchor=SE, fill='orange')

Tags: textselfcityfordefbuttonwindowframe