更好地理解 tkinter-Python 中的组合框 - 如何在 tkinter 中获取选定的值

Better understanding combox in tkinter-Python - How to get the selected value in tkinter

我正在努力学习 Python,但我一直很难理解以下问题:

我的目标是收集用户在组合框中选择的值。就在下面,我放了一段我放在一起的程序。然而,一个疑问出现了: 在 PageOne Class - selected 函数中,为什么代码“list_1.append (var.get ())”有效而代码“list_1 = (var.get ( ))" 不起作用 ???

另外,这部分代码中,对list_1变量的任何操作也是不可用的,指出错误:UnboundLocalError: local variable 'list_1' referenced before assignment.

你能帮我更好地理解这个问题吗?

from tkinter import *
from tkinter import ttk

list_1 = []

class Validation_Tool(Tk):

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        for F in (PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("PageOne")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

    def quit(self):
        self.destroy()

class PageOne(Frame):

        # Combobox event handler. ADDED
    def selected(self, event, var):
        # list_1=(var.get())        # DOES NOT WORK !!!!!!!!!!!!!!!!!
        list_1.append(var.get())    # IT WORKS!!!!!!!!!!!!

    def button(self, list_1):
        print(list_1)

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        combobox_var = StringVar()  # ADDED.
        combobox = ttk.Combobox(self, values=list('abcde'),
                                textvar=combobox_var)  # For each Combobox. ADDED.
        combobox.grid(row=1, column=0)
        combobox.bind('<<ComboboxSelected>>',  # Bind event handler.           ADDED.
                        lambda event, var=combobox_var: self.selected(event, var))  # ADDED.

        quit_button = Button(self, text="Quit Program",
                            command=lambda: controller.quit())
        next_button = Button(self, text='Show options', command= lambda: self.button(list_1))

        quit_button.place(relx=0.98, rely=0.98, anchor=SE)
        next_button.place(relx=0.76, rely=0.98, anchor=SE)

class PageTwo(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller


if __name__ == "__main__":
    root = Validation_Tool()
    root.geometry('400x300+430+250')
    root.title("Validation Tool")
    root.mainloop()

我想我已经修好了:

from tkinter import ttk
import tkinter as tk


list_1 = None

class Validation_Tool(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        for FrameClass in (PageOne, PageTwo):
            page_name = FrameClass.__name__
            frame = FrameClass(parent=container, controller=self)
            self.frames.update({page_name: frame})

            frame.grid(row=0, column=0, sticky="news")

        self.show_frame("PageOne")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

    def quit(self):
        self.destroy()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller

        self.combobox_var = tk.StringVar()
        self.combobox = ttk.Combobox(self, values=list("abcde"), textvar=self.combobox_var)
        self.combobox.grid(row=1, column=0)
        self.combobox.bind("<<ComboboxSelected>>", self.selected)

        quit_button = tk.Button(self, text="Quit Program", command=self.controller.quit)
        next_button = tk.Button(self, text="Show options", command=self.button)

        quit_button.place(relx=0.98, rely=0.98, anchor="se")
        next_button.place(relx=0.76, rely=0.98, anchor="se")

    def selected(self, event):
        # `list_1` need to be global as we are assigning a value to it
        global list_1
        list_1 = self.combobox_var.get()

    def button(self):
        print(list_1)


class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller


if __name__ == "__main__":
    root = Validation_Tool()
    root.geometry("400x300+430+250")
    root.title("Validation Tool")
    root.mainloop()

我所要做的就是将绑定更改为:

self.combobox.bind("<<ComboboxSelected>>", self.selected)

以及方法:

def selected(self, event):
    # `list_1` need to be global as we are assigning a value to it
    global list_1
    list_1 = self.combobox_var.get()

如果要为全局对象赋值,您需要使用 global list_1 告诉 python 将该对象视为全局变量。我还让你的代码更 pythonic.