tkinter 两个组合框总是获得相同的值

tkinter two combobox always getting the same value

我正在尝试构建一个 GUI 应用程序,我处于早期阶段。

import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.title("Test")
        self.geometry("760x250")
        self.inter_sources = ['a', 'b', 'c', 'd']
        self.inter_destinations = ['a', 'b', 'c', 'd']
        self.source_options = ttk.Combobox(self, state="readonly", width=40,
                                           values=self.inter_sources, textvariable=self.inter_sources)
        self.destination_options = ttk.Combobox(self, state="readonly", width=40,
                                                values=self.inter_destinations, textvariable=self.inter_destinations)

    def place_options(self):
        self.source_options.place(x=10, y=40)
        self.source_options.set(self.inter_sources[0])
        self.destination_options.place(x=10, y=80)
        self.destination_options.set(self.inter_destinations[0])


x = App()
x.place_options()
x.mainloop()

出于某种原因,每当 source_optionsdestination_options 中的任何一个更改值时,另一个都会采用相同的值,我不确定这是一个错误还是我做错了什么。 我想要的是正常操作发生在一个改变另一个不改变的地方。

感谢任何帮助。

textvariable= 不适用于分配所有值的列表。

用于分配StringVar()(或类似对象)以获取或设置Combobox中的选定值。 tkinter 使用带有 ID 的字符串来识别分配的 StringVar.

当您使用相同的列表时,它会转换为相同的字符串,并且 Combobox 都获得相同的 ID(并且可能会自动为该 ID 创建 StringVar - 但我无法确认. tkinter 以我不使用的语言 tk 作为代码运行。
当您更改一个 Combobox 中的选择时,它会更改 StringVar 中的值,这会自动更改使用相同 ID 的所有 Combobox 中的选择。

您应该创建两个 StringVar 并分配给不同的 Combobox

self.inter_sources = ['a', 'b', 'c', 'd']
self.inter_destinations = ['a', 'b', 'c', 'd']

self.selected_source = tk.StringVar(self)
self.selected_destination = tk.StringVar(self)

self.source_options = ttk.Combobox(self, state="readonly", width=40,
                                   values=self.inter_sources,
                                   textvariable=self.selected_source
                                   )

self.destination_options = ttk.Combobox(self, state="readonly", width=40,
                                        values=self.inter_destinations,
                                        textvariable=self.selected_destination
                                        )

但是如果你只想从 Combobox 中获取值那么你不需要 textvariable 因为你可以直接从 Combobox

中获取它

带有按钮的示例代码,该按钮运行代码以从所有 Combobox

获取值
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.title("Test")
        self.geometry("760x250")
        
        self.inter_sources = ['a', 'b', 'c', 'd']
        self.inter_destinations = ['a', 'b', 'c', 'd']
        
        self.selected_source = tk.StringVar(self)
        self.selected_destination = tk.StringVar(self)
        
        self.source_options = ttk.Combobox(self, state="readonly", width=40,
                                           values=self.inter_sources,
                                           textvariable=self.selected_source
                                           )
        
        self.destination_options = ttk.Combobox(self, state="readonly", width=40,
                                                values=self.inter_destinations,
                                                textvariable=self.selected_destination
                                                )
     
        tk.Button(self, text='Check', command=self.on_press).pack()
        
    def on_press(self):
        print('selected_source:', self.selected_source.get())
        print('source_options :', self.source_options.get())
        print('selected_destination:', self.selected_destination.get())
        print('destination_options :', self.destination_options.get())

    def place_options(self):
        self.source_options.place(x=10, y=40)
        self.source_options.set(self.inter_sources[0])
        self.destination_options.place(x=10, y=80)
        self.destination_options.set(self.inter_destinations[0])


x = App()
x.place_options()
x.mainloop()