I have a main class for my GUI, where i create a 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()
I have a class for each page of my Notebook and i want update my progressbar when i run a function in my page2, I tried:
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()
But I get the error message :
AttributeError: 'Page2' object has no attribute 'progress'
I simplified the code for be the most generalist possible.
How can I do that then?
CodePudding user response:
You have progressbar
in MainApplication
so Page2
would need some access to MainApplication
.
Normally we send parent object as first argument in widgets i.e. Button(root,...)
and later widget can use self.master
to access parent object.
But you don't assign parent object in super().__init__()
so it automatically set tk.Tk
as parent. And if you use self.master
then you should have access to MainApplication
self.master.progressbar.start()
# etc.
Page2
|
| .master
V
MainApplication
EDIT:
If you would assign Notebook
(container
) as master/parent for Page2
class Page2(ttk.Frame):
def __init__(self, container):
super().__init__(container) # <-- `container` as parent
then you would need
self.master.master.progressbar.start()
# etc.
Page2
|
| .master
V
Notebook
|
| .master
V
MainApplication