我正在嘗試構建一個 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_options或destination_options更改值時,另一個將采用相同的值,我不確定這是一個錯誤還是我做錯了什么。我想要的是正常操作發生在一個改變另一個不改變的地方。
任何幫助表示贊賞。
uj5u.com熱心網友回復:
textvariable=不適用于分配所有值的串列。
它用于分配StringVar()(或類似物件)以獲取或設定中的選定值Combobox。并且 tkinter 使用帶有 ID 的字串來識別分配StringVar的。
當您使用相同的串列時,它會轉換為相同的字串并且都Combobox獲得相同的 ID(并且可能會StringVar為此 ID 自動創建 - 但我無法確認。以我不使用tkinter的語言作為代碼運行)。
當您將選擇更改為一個時,它會更改值,其中會自動更改所有使用相同 ID 的選擇。tkComboboxStringVarCombobox
您應該創建兩個StringVars 并分配給不同Combobox的 s
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
帶有按鈕的示例代碼,該代碼運行代碼以從所有人中獲取 vlauesCombobox
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446459.html
