我正在 tkinter 中創建一個需要額外輸入名稱的視窗。為了獲得這個輸入,我創建了一個特殊的訊息框來獲取輸入并有一個按鈕來確認輸入中的輸入。然后我需要從類中獲取該變數作為回傳值。問題是我不知道如何獲得這樣的價值,當用戶可以花他們希望的時間。我應該怎么辦?(我需要回傳的值是extension_name)
我當前的代碼:
class ExtensionNamer(Toplevel):
def __init__(self, master, x, y, *args, **kwargs):
#Initialize the window
Toplevel.__init__(self, master, *args, **kwargs)
#Set basic window properties
self.title("Extension Naming")
self.geometry("300x100 {} {}".format(x, y))
self.attributes("-topmost", True)
self.resizable(False, False)
self.grab_set()
#Create the widgets
self.extension_name_label = Label(self, text="Extension Name:", font=medium_font)
self.extension_name_label.place(relx=0.5, rely=.15, anchor=CENTER)
self.extension_name_entry = Entry(self, font=normal_font, width=20, justify=CENTER)
self.extension_name_entry.place(relx=0.5, rely=.5, anchor=CENTER)
self.confirm_button = Button(self, text="Confirm", font=normal_font, command=self.confirm)
self.confirm_button.pack(anchor=S, side=LEFT)
self.cancel_button = Button(self, text="Cancel", font=normal_font, command=self.cancel)
self.cancel_button.pack(anchor=S, side=RIGHT)
self.protocol("WM_DELETE_WINDOW", self.cancel)
def confirm(self):
#Confirm the extension name
self.extension_name = self.extension_name_entry.get()
if ".xt" not in self.extension_name:
self.extension_name = ".xt"
self.destroy()
def cancel(self):
#Cancel the extension name
self.extension_name = "NewXT.xt"
self.destroy()
...
def add_tab(self, event=None):
#Adds a new tab
tab_name = ExtensionNamer(self, self.winfo_x(), self.winfo_y(), color_mode=self.color_mode)
uj5u.com熱心網友回復:
向類添加回呼函式引數。當用戶確認分機時,會以分機名稱呼叫該函式。
這類似于 Tkinter 小部件獲取command引數的方式,并且您使用它的方式相同。呼叫時ExtensionNamer(),添加一個callback=引數,指定在用戶確認后將使用擴展名的函式。
class ExtensionNamer(Toplevel):
def __init__(self, master, x, y, callback, *args, **kwargs):
#Initialize the window
super().__init__(self, master, *args, **kwargs)
self.callback = callback
#Set basic window properties
self.title("Extension Naming")
self.geometry("300x100 {} {}".format(x, y))
self.attributes("-topmost", True)
self.resizable(False, False)
self.grab_set()
#Create the widgets
self.extension_name_label = Label(self, text="Extension Name:", font=medium_font)
self.extension_name_label.place(relx=0.5, rely=.15, anchor=CENTER)
self.extension_name_entry = Entry(self, font=normal_font, width=20, justify=CENTER)
self.extension_name_entry.place(relx=0.5, rely=.5, anchor=CENTER)
self.confirm_button = Button(self, text="Confirm", font=normal_font, command=self.confirm)
self.confirm_button.pack(anchor=S, side=LEFT)
self.cancel_button = Button(self, text="Cancel", font=normal_font, command=self.cancel)
self.cancel_button.pack(anchor=S, side=RIGHT)
self.protocol("WM_DELETE_WINDOW", self.cancel)
def confirm(self):
#Confirm the extension name
self.extension_name = self.extension_name_entry.get()
if ".xt" not in self.extension_name:
self.extension_name = ".xt"
self.destroy()
self.callback(self.extension_name)
你會像這樣使用它:
def add_tab(self, event=None):
#Adds a new tab
tab_name = ExtensionNamer(self, self.winfo_x(), self.winfo_y(), callback=self.display_name, color_mode=self.color_mode)
def display_name(self, name):
print(f'Extension name is {name}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492001.html
