Ajay Singh Rana
Ajay Singh Rana

Reputation: 573

Why does the overriden method act weirdly..?

I have the following code:

import tkinter as tk
from tkinter import  filedialog
import time

class Some:
    def __init__(self):
        self.filename = ''
    
    def browse(self):
        filename = filedialog.askopenfilename()
        self.label.config(text = filename)

class App(tk.Tk,Some):
    def __init__(self):
        tk.Tk.__init__(self)
        Some.__init__(self)
        
        self.label = tk.Label(self,text = '--Empty Selection--')
        self.label.grid(row = 0,column = 0,padx = 10, pady = 30)
        
        button = tk.Button(self,text = 'Select File',command = self.browse)
        button.grid(row = 0,column = 1,padx = 10,pady = 30)
        
app = App()
app.mainloop()

This works fine.But when I try to override the browse method in order to give it extra functionality an unexpected behaviour is observed. In the overridden method the time.sleep(2) is executed first and only after that i get the result of Some.browse(self).What is the explanatioin behind this behaviour and how can I fix it to work as expected i.e. Some.browse(self) executes first and time.sleep(2) executes second..?

import tkinter as tk
from tkinter import  filedialog
import time

class Some:
    def __init__(self):
        self.filename = ''
    
    def browse(self):
        filename = filedialog.askopenfilename()
        self.label.config(text = filename)

class App(tk.Tk,Some):
    def __init__(self):
        tk.Tk.__init__(self)
        Some.__init__(self)
        
        self.label = tk.Label(self,text = '--Empty Selection--')
        self.label.grid(row = 0,column = 0,padx = 10, pady = 30)
        
        button = tk.Button(self,text = 'Select File',command = self.browse)
        button.grid(row = 0,column = 1,padx = 10,pady = 30)
    
    def browse(self):
        Some.browse(self)
        time.sleep(2)
        
app = App()
app.mainloop()

Upvotes: 0

Views: 43

Answers (1)

Ajay Singh Rana
Ajay Singh Rana

Reputation: 573

As suggested by acw1668 the time.sleep(2) function was blocking tkinter from updating the label and as he suggested self.label.update() fixed the problem.

import tkinter as tk
from tkinter import  filedialog
import time

class Some:
    def __init__(self):
        self.filename = ''
    
    def browse(self):
        filename = filedialog.askopenfilename()
        self.label.config(text = filename)

class App(tk.Tk,Some):
    def __init__(self):
        tk.Tk.__init__(self)
        Some.__init__(self)
        
        self.label = tk.Label(self,text = '--Empty Selection--')
        self.label.grid(row = 0,column = 0,padx = 10, pady = 30)
        
        button = tk.Button(self,text = 'Select File',command = self.browse)
        button.grid(row = 0,column = 1,padx = 10,pady = 30)
    
    def browse(self):
        Some.browse(self)
        self.label.update()    # this lines fixes the problem
        time.sleep(2)
        
app = App()
app.mainloop()

Upvotes: 1

Related Questions