我有 3 個相當大的 tkinter 檔案,每個檔案都運行良好,但希望將它們放在一個螢屏上:如何使用我最近從這個論壇 (TY) 學到的頁面。現在,我希望在單獨的 .py 檔案中完成單獨頁面的大部分編碼。但是,特別是在小部件的情況下,我無法弄清楚如何在單獨的 .py 檔案中使用框架名稱。我確實意識到,正如我所嘗試的那樣,標簽創建的功能是
方式縮短示例:
主檔案
from functions import *
root = tk.Tk()
root.title("Kombucha Program")
root.geometry("1400x800")
root.minsize(width=900, height=600)
#root.maxsize(width=1400, height = 900)
root.grid_rowconfigure(3, weight=1)
root.grid_columnconfigure(2, weight=1)
root.configure( bg = '#000080' )
theFrame = tk.Frame(root, width=1200, height = 630, bg = 'yellow') #0059b3')
theFrame.grid(column=0,row=1, sticky = N, in_ = root)
getlabels()
mainloop()
函式.py
def getLabels():
exitButton = tk.Button(theFrame, text="Quit the Program", width = 12, command=root.destroy)
exitButton.grid(row = 0, column = 0)
uj5u.com熱心網友回復:
就您有問題的代碼而言,您必須將框架作為引數傳遞給函式。
def getLabels(theFrame):
root = theFrame.master # Get the parent window from the frame itself
exitButton = tk.Button(theFrame, text="Quit the Program", width = 12, command=root.destroy)
exitButton.grid(row = 0, column = 0)
...并在呼叫它時,傳遞所需的幀:
getlabels(theFrame)
另請注意,我root從框架中提取,因為root它不在functions.py. 但老實說,如果您使用類并將每個框架作為一個類,您可以使專案的結構更好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402460.html
標籤:
