-1

I have a problem with a GUI I created, that includes a label that indicates that the program is working in the background, the real script processes a huge amount of data so I want that indicator to tell the user it is indeed doing something. It worked fine so far like this, displaying "Idle" at script start, "running" while the function runs its course, and "Done" when it's done. (code is only the relevant part):

from tkinter import*

def make_pause_function(sleepytime):
    import time
    print("Falling asleep")
    time.sleep(sleepytime)
    print("Awakening")

class MyGUI:

   def __init__(self):
    self.__mainWindow = Tk()
    self.header = StringVar()
    self.header.set(3)
    self.labelText = 'Idle'
#    self.Button2 = Button(text = "Sleep for X seconds!", command = self.run_main_script).grid(row=1,column=0)
#    self.Entry2 = Entry(self.__mainWindow, textvariable=self.header, width = 100).grid(row=2,column=0)
#    self.Label3 = Label(self.__mainWindow, text = self.labelText).grid(row=3,column=0)

    self.Button2 = Button(text = "Sleep for X seconds!", command = self.run_main_script)
    self.Entry2 = Entry(self.__mainWindow, textvariable=self.header, width = 100)
    self.Label3 = Label(self.__mainWindow, text = self.labelText)

    self.Button2.pack()
    self.Entry2.pack()
    self.Label3.pack()

    mainloop()

   def run_main_script(self):
    self.Label3["text"] = 'running'
    self.__mainWindow.update_idletasks()
    header=self.header.get()
    make_pause_function(int(header))
    self.Label3["text"] = 'done'
    self.__mainWindow.update_idletasks()

myGUI = MyGUI()

But when the GUI grew because of many options, I switched from pack to grid geometry manager and then the label updating I had gotten working after a lot of trial and error got broken again. The follwing code doesn't work:

from tkinter import*

def make_pause_function(sleepytime):
    import time
    print("Falling asleep")
    time.sleep(sleepytime)
    print("Awakening")

class MyGUI:

   def __init__(self):
    self.__mainWindow = Tk()
    self.header = StringVar()
    self.header.set(3)
    self.labelText = 'Idle'
    self.Button2 = Button(text = "Sleep for X seconds!", command = self.run_main_script).grid(row=1,column=0)
    self.Entry2 = Entry(self.__mainWindow, textvariable=self.header, width = 100).grid(row=2,column=0)
    self.Label3 = Label(self.__mainWindow, text = self.labelText).grid(row=3,column=0)

#    self.Button2 = Button(text = "Sleep for X seconds!", command = self.run_main_script)
#    self.Entry2 = Entry(self.__mainWindow, textvariable=self.header, width = 100)
#    self.Label3 = Label(self.__mainWindow, text = self.labelText)

#    self.Button2.pack()
#    self.Entry2.pack()
#    self.Label3.pack()

    mainloop()

   def run_main_script(self):
    self.Label3["text"] = 'running'
    self.__mainWindow.update_idletasks()
    header=self.header.get()
    make_pause_function(int(header))
    self.Label3["text"] = 'done'
    self.__mainWindow.update_idletasks()

myGUI = MyGUI()

Seems that update_idletasks doesn't like grid. But I don't like pack for a GUI with lots of buttons and fields. Any way to do what I want with grid packing?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160

2 Answers2

0

try this:

self.Label3 = Label(self.__mainWindow, text = self.labelText).grid(row=3,column=0)

from that, to this:

self.Label3 = Label(self.__mainWindow, text = self.labelText)
self.Label3.grid(row=3,column=0)
rn0mandap
  • 333
  • 1
  • 8
0

The follwing line causes the error:

self.Label3 = Label(self.__mainWindow, text = self.labelText).grid(row=3,column=0)

grid() is a function that doesn't return anything (None). Because of that, you're saving in variable self.Lable3 nothing. Then, when you run the line self.Label3["text"] = 'running' an error pops up because self.Lable3 is None. In order to solve it, seperate those lines- first save the Label in a variable and then use the grid() function on it:

self.Label3 = Label(self.__mainWindow, text = self.labelText)
self.Label3.grid(row=3,column=0)

By the way, I recommend using place() method instead of grid() because in my opinion it is easier to place objects with it. You can read about it Here