[tkinter] status window in seperate thread help

Post here if you need help with creating a Graphical User Interface in Python.

[tkinter] status window in seperate thread help

Postby stchman » Wed May 07, 2014 12:06 am

Using Python 3.

Hello all.
I am trying to create a status window in a Python program. I figured I would launch the tkinter status window in a new thread.
Here is some example code.

Original calling program:
Code: Select all
import os
import sys
import tkinter
import subprocess


class Status_Window:
    def Build():
        try:
            print('here')
            command = 'cmd /c start python ' + os.path.join(r'D:\Python3\Process', 'status_window.py')
            print('command = ', command)
            pid = subprocess.Popen(command)
        except:
            print('exception')


# main function
def main():
    Status_Window.Build()


# call main
if __name__ == "__main__":
   main()

Program the first program calls:
Code: Select all
import sys
import time
import tkinter
import threading
import subprocess


class Window:
    def Status_Window():
        # make the Text area global
        global Output_Text
        global Status_GUI

        # create GUI object
        Status_GUI = tkinter.Tk()

        # set the GUI backgroung color to grey
        Status_GUI.configure(bg = "grey")

        # create GUI size and center
        Window.Center_GUI(Status_GUI, 600, 300)

        # create a Text window inside the new popup GUI
        Output_Text = tkinter.Text(Status_GUI, bd = 4, height = 19, width = 80, font = ("Courier", 8))
        Output_Text.pack()
        Output_Text.place(x = 10, y = 10)

        # needed for Windows to keep GUI up and running
        Status_GUI.mainloop()


    def Center_GUI(GUI, w, h):
        # get screen width and height
        ws = GUI.winfo_screenwidth() # This value is the width of the screen
        hs = GUI.winfo_screenheight() # This is the height of the screen

        # calculate position offsets x and y
        x = int(ws / 2) - int(w / 2)
        y = int(hs / 2) - int(h / 2)

        # center GUI in screen
        GUI.geometry("{:d}x{:d}-{:d}-{:d}".format( w,h,x,y ))       
 

def main():
    #Window.Status_Window()
   
    print('before thread')
    t = threading.Thread(target = Window.Status_Window())
    t.start()
    print('after thread')
 
   
# call main
if __name__ == "__main__":
   main()

When I run the code, the GUI pops up as expected, but then the program freezes. I would have expected that launching the code in another thread would alleviate it. Also I would like to be able to update the status window as the program runs.
Thanks.
Last edited by Yoriz on Wed May 07, 2014 12:13 am, edited 1 time in total.
Reason: First post lock, Changed Title.
stchman
 
Posts: 1
Joined: Tue May 06, 2014 11:52 pm

Re: [tkinter] status window in seperate thread help

Postby KillerTomatoes » Mon Jun 23, 2014 7:20 pm

My first impression is that there might me typo errors, but I don't know if the code you posted was *exactly* what you are using, or a retyped copy. What I mean is that in the main() function, you have the line: #Window.Status_Window() right after the main() declaration. Was the comment symbol intentional? Also, you have a class named Status_Window AND a method called Status_Window; using the same name for both is a recipe for confusion. One more thing that may (or may not ) be relevant: Python programs don't start with the main() function; if you wrote the code assuming that the program would start at the main() function, you will be disappointed.
Last edited by Mekire on Tue Jun 24, 2014 11:37 am, edited 1 time in total.
Reason: First post lock.
KillerTomatoes
 
Posts: 1
Joined: Mon Jun 23, 2014 6:19 pm


Return to GUI

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron