I am not able to get the ttk.Progressbar widget to work. May I know what is the issue and how I can fix it?
I know the Progressbar widget is functional; whe
This is what you currently have in your code:
you set self.espconnecting = False
you call _connect_esp()
which calls _show_conn_progress()
which sets self.espconnecting = True and starts the progressbar self.sp_pbar.start()
and then calls _update_conn_progress()
which checks the value of self.espconnecting.
If self.espconnecting is True(which it currently is) connection continues and progress bar keeps rolling as expected.
If self.espconnecting is False progress bar is stopped self.sp_pbar.stop()
Before .after() can make it's callback in 500ms, Control is passed back to _connect_esp which sets self.espconnecting = False.
Then .after() calls _update_conn_progress() which is meant to keep the bar rolling,
but(Here is your problem): what is the last value of self.espconnecting? =False hence, control branches to self.sp_pbar.stop(), which stops the progrss bar. This is why when you comment that line out your code works as expected, because even if control branches there, there will be nothing to prevent the progress bar from working.
SOLUTION
Do not set self.espconnecting = False in _connect_esp() because before .after() makes it's callback in 500ms, control would have been passed back to _connect_esp() which sets self.espconnecting = False which prevents your progress bar from working.
This means you have to find another means to 'end the connection', once it gets started.
N.B: I really don't see the need for time.sleep(5) in the code.
Here is a possible way to go about the solving it:
...
def __init__( self, master=None, *args, **kw ):
super().__init__( master,style='App.TFrame')
self.master = master
self.espconnecting = False
self.count=0
self._set_style()
self._create_widgets()
def _set_style( self ):
print( '\ndef _set_style( self ):' )
self.style = ttk.Style()
self.style.configure( 'App.TFrame', background='pink')
self.style.configure( 'sp.TFrame', background='light green')
def _create_widgets( self ):
print( '\ndef _create_widgets( self ):' )
self.sp_frame = ttk.Frame( self, style='sp.TFrame' )
self.sp_frame.grid(row=0, column=0)
#self.sp_frame widgets
self.sp_label1 = ttk.Label( self.sp_frame, text='SP(s):')
self.sp_label2 = ttk.Label( self.sp_frame, text='ESP(s):')
self.sp_label3 = ttk.Label( self.sp_frame, )
self.sp_combox = ttk.Combobox( self.sp_frame, state="readonly",
values=['a','b','c'] )
self.sp_combox.bind('<>', self._connect_esp)
self.sp_pbar = ttk.Progressbar( self.sp_frame, length=200,
mode='indeterminate',
orient=tk.HORIZONTAL, )
self.sp_label1.grid( row=0, column=0 )
self.sp_combox.grid( row=0, column=1, padx=[10,0] )
self.sp_pbar.grid( row=1, column=0, columnspan=2, sticky='ew' )
self.sp_label2.grid( row=2, column=0)
self.sp_label3.grid( row=2, column=1)
def _connect_esp( self, event=None):
print( '\ndef connect_esp( self, event=None ):' )
self._show_conn_progress()
print("START Connection")
time.sleep(5)
def end_connection(self):
print("END Connection")
self.espconnecting = False
def _show_conn_progress( self ):
print( '\ndef _show_conn_progress( self ):' )
self.espconnecting = True
self.sp_label3['text']='Connecting.....'
self.sp_label3.update_idletasks()
self.sp_pbar.start()
self._update_conn_progress()
def _update_conn_progress( self ):
print( '\ndef _update_conn_progress( self ):' )
if not self.espconnecting:
print('connected')
self.sp_pbar.stop()
self.sp_label3['text']='Connected'
else:
print('connecting')
#self.sp_pbar.update_idletasks()
self.after(500, self._update_conn_progress) # Call this method after 500 ms.
self.count=self.count + 1
if self.count==10:
self.end_connection()
def main():
root = tk.Tk()
root.geometry('300x100+0+24')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
app = App( root )
app.grid(row=0, column=0, sticky='nsew')
root.mainloop()
if __name__ == '__main__':
main()