我的 GUI 有一個主類,我在其中創建一個 ttk.ProgressBar:
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
#--Fenêtre principale
self.title("MyApp")
self.geometry('1000x500')
self.notebook = ttk.Notebook(self)
self.Page1 = Page1(self.notebook)
self.Page2 = Page2(self.notebook)
self.Page3 = Page3(self.notebook)
self.Page4 = Page4(self.notebook)
self.notebook.add(self.Page1, text='Page1')
self.notebook.add(self.Page2, text='Page2')
self.notebook.add(self.Page3, text='Page3')
self.notebook.add(self.Page4, text='Page4')
self.notebook.pack(fill=BOTH, expand=True)
self.progress = ttk.Progressbar(self, orient=HORIZONTAL, length=490, mode='determinate')
self.progress.pack()
我的筆記本的每一頁都有一個類,我想在我的 page2 中運行一個函式時更新我的??進度條,我試過:
class Page2(ttk.Frame):
def __init__(self, container):
super().__init__()
self.send = ttk.Button(self, text='SEND', command=send_message)
self.Button_envoyer.place(relx=0.01, rely=0.8)
def send_message(self):
self.progress.start()
self.progress['value'] = 0
self.update_idletasks()
self.time.sleep(1)
print("0%")
self.progress['value'] = 50
self.update_idletasks()
self.time.sleep(1)
print("50%")
self.progress['value'] = 100
self.update_idletasks()
self.time.sleep(1)
print("100%")
self.progress.stop()
但我收到錯誤訊息:
AttributeError: 'Page2' object has no attribute 'progress'
我簡化了代碼,以便盡可能地通才。
那我該怎么做呢?
uj5u.com熱心網友回復:
你有progressbar,MainApplication所以Page2需要一些訪問權限MainApplication。
通常我們將父物件作為小部件中的第一個引數發送,即Button(root,...)以后的小部件可以self.master用來訪問父物件。
但是您沒有分配父物件,super().__init__()因此它會自動設定為父物件tk.Tk。如果您使用self.master,那么您應該可以訪問MainApplication
self.master.progressbar.start()
# etc.
Page2
|
| .master
V
MainApplication
編輯:
如果您將Notebook( container) 指定為主/父Page2
class Page2(ttk.Frame):
def __init__(self, container):
super().__init__(container) # <-- `container` as parent
那么你需要
self.master.master.progressbar.start()
# etc.
Page2
|
| .master
V
Notebook
|
| .master
V
MainApplication
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/452616.html
