我創建了一個筆記本并為其添加了一個框架:
nb = ttk.Notebook(root, style="TNotebook")
page1 = ttk.Frame(nb, style='Frame1.TFrame')
layout1(page1)
nb.add(page1, text='Welcome')
所以我有一個函式 layout1,筆記本的第一頁,我添加了一個文本:
def layout1(page):
entry = Text(page, width=20)
entry.place(relx=0.03, rely=0.1, height=400)
Button(page, text='EXECUTE', command=import_entry).place(relx=0.5, rely=0.6)
接下來我有我的 import_entry 功能:
def import_entry():
result = entry.get()
print(result)
由于函式中變數的可訪問性,我無法獲得該條目。那么,我怎樣才能得到它?
uj5u.com熱心網友回復:
下面是一個示例,說明您應該如何使用類來構建您的應用程式:
import tkinter
import tkinter.ttk as ttk
class App(tkinter.Tk):
def __init__(self):
super().__init__()
# assign on_closing method to window close event
self.protocol("WM_DELETE_WINDOW", self.on_closing)
self.title("Example App")
self.geometry("600x500")
self.button_1 = tkinter.Button(master=self, text="Test", command=self.button_event)
self.button_1.pack(pady=10)
# create more widgets ...
def button_event(self, event):
print("button pressed")
def on_closing(self):
# code that needs to happen when gets closed
self.destroy() # controlled closing of window with .destroy()
if __name__ == "__main__":
app = App()
app.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/448495.html
