SirParselot
SirParselot

Reputation: 2700

Tkinter window doesn't display anything

I have a program that is very slow to load and has no indication that it is loading once clicked on so I am trying to make a window pop up that says "Loading...Please Wait" and has a progress bar. I'm trying to use threads ,which I kind of understand, but I'm not very good at using them. My problem is that when my window pops up it's not displaying anything other than a default tk window. I think my problem has to do when I am calling root.mainloop or root.update. Here's my code

from Tkinter import *
from PIL import Image, ImageTk
import Tkinter
import ttk
import subprocess
import threading
import time

class MainUI:

    def __init__(self,parent):
        self.parent = parent
        self.counter = IntVar()
        self.main_container = Frame(self.parent)
        self.main_container.pack()
        self.startThread

    def center():
        self.parent.update_idletasks()
        w = self.parent.winfo_screenwidth()
        h = self.parent.winfo_screenheight()
        size = tuple(int(_) for _ in self.parent.geometry().split('+')[0].split('x'))
        x = w/2 - size[0]/2
        y = h/2 - size[1]/2
        self.parent.geometry("%dx%d+%d+%d" % (size + (x, y)))
        self.parent.lift()


    def progress_bar():
        ft = ttk.Frame()
        ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)
        pb = ttk.Progressbar(ft, orient='horizontal', mode='determinate', variable = self.counter, maximum=100)
        pb.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.BOTTOM)
        pb.start(20)
        label = Tkinter.Label(self.parent, Text='Loading...Please Wait') # code before computation starts
        label.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)
        self.center
        self.parent.lift()
        self.parent.wm_title("")
        self.parent.update()

    def startThread(self):
        def btn_start_click():
            self.progress_bar()
            for i in range(101):
                self.counter.set(i)
                time.sleep(1)
                self.parent.update()
        t = threading.Thread(None, btn_start_click, ())
        t.start()
        self.parent.update()


def op():
    ##p1 = subprocess.Popen(['program to open'], stdout=None)
    print()

def Main():
    root = Tkinter.Tk()
    t1=threading.Thread(target=op)
    t1.start()
    MainUI(root)
    root.mainloop()
    t1.join()

if __name__ == '__main__':
    Main()

Upvotes: 1

Views: 364

Answers (1)

SirParselot
SirParselot

Reputation: 2700

Alright I figured out my problem. The first thing I changed was moving the label part into main and the first thing I needed to change was to add () after btn_start_click. Second, I needed to update root after update self.counter. Third, I needed to destroy my window when it was done. I think that's everything I changed. Here's my working code if I didn't mention everything.

from Tkinter import *
from PIL import Image, ImageTk
import Tkinter
import ttk
import subprocess
import threading
import time

class MainUI:
    def __init__(self,parent):
        self.parent = parent
        self.counter = IntVar()
        self.startThread()

    def center(self):
        self.parent.update_idletasks()
        w = self.parent.winfo_screenwidth()
        h = self.parent.winfo_screenheight()
        size = tuple(int(_) for _ in self.parent.geometry().split('+')[0].split('x'))
        x = w/2 - size[0]/2
        y = h/2 - size[1]/2
        self.parent.geometry("%dx%d+%d+%d" % (size + (x, y)))

    def progress_bar(self):
        ft = ttk.Frame()
        ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)



        pb = ttk.Progressbar(ft, orient='horizontal', mode='determinate', variable = self.counter, maximum=45)
        print(self.counter)
        pb.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.BOTTOM)

        self.center()
        self.parent.lift()
        self.parent.title("")


    def startThread(self):
        def btn_start_click():
            self.progress_bar()
            for i in range(46):
                self.counter.set(i)
                self.parent.update()
                print(i)
                time.sleep(1)

        t = threading.Thread(target = btn_start_click())
        t.start()
        self.parent.destroy()

def op():
    ##p1 = subprocess.Popen(['program to open'], stdout=None)
    print()

def Main():
    root = Tkinter.Tk()
    label = Tkinter.Label(root, Text = 'Loading...Please Wait') # code before computation starts
    label.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)

    t1=threading.Thread(target=op())
    t1.start()
    MainUI(root)
    t1.join()

if __name__ == '__main__':
    Main()

Upvotes: 1

Related Questions