Mysterylz
Mysterylz

Reputation: 1

Python 2.7 Tkinter is not response when run program

I'm absolute beginner for python Tkinter. My program has serial port and TCP client socket connection (Running in thread). It's running well in console application but not work in Tkinter GUI.

count = 0
initialState = True

def initState(reader, ReaderName, readerType, serialport, baud, databit, readerPacket):
    global count
    global initialState
    if initialState:
        while not reader.SettingReader(ReaderName, readerType, serialport, baud, databit, readerPacket):
            count += 1
        count = 0
        labelSearching.place(x=290, y=260)
        labelReaderSetting.configure(image=readerSettingSuccess)
        app.update_idletasks()

        labelSearching.grid_forget()
        labelReaderConnect.place(x=290, y=260)
        app.update_idletasks()

        labelReaderConnect.configure(image=readerConnected)
        labelServerConnect.place(x=290, y=320)
        app.update_idletasks()

        while not reader.StartServer():
            count += 1
        count = 0
        labelServerConnect.configure(image=serverConnected)
        app.update_idletasks()

        labelContainer.grid_forget()
        labelReaderSetting.configure(image=readerSettingSuccessSmall)
        labelReaderSetting.place(x=80, y=200)
        labelReaderSetting.lift()
        labelReaderConnect.configure(image=readerConnectedSmall)
        labelReaderConnect.place(x=80, y=260)
        labelReaderConnect.lift()
        labelServerConnect.configure(image=serverConnectedSmall)
        labelServerConnect.place(x=80, y=320)
        labelServerConnect.lift()
        labelWaitingTap.place(x=460, y=260)
        labelLeft.grid(row=1, column=0)
        labelRight.grid(row=1, column=1)
        app.update_idletasks()
        reader.SaveSettingToFile()
        initialState = False
    else:
        runnMainProgram(reader)
    app.update()
    app.after(1000, functools.partial(initState, reader, ReaderName, readerType, serialport, baud, databit, readerPacket))

def runnMainProgram(reader):
    try:
        check = reader.StartReader(reader._CARDANDPASSWORD)
        app.update_idletasks()
        if check == True:
            print "Open the door"
            check = ""
            print "Ready..."
            app.update_idletasks()
        elif check == False:
            print "Doesn't Open The Door"
            check = ""
            print "Ready..."
            app.update_idletasks()
        elif check == 2:
            print "Reader disconnect"
            print "Reconnecting to Reader"
            reader.ClosePort()
            while not reader.OpenPort():
               count += 1
               count = 0
            check = ""
            print "Ready..."
            app.update_idletasks()
    except KeyboardInterrupt:
        exit()
    app.after(10, functools.partial(runnMainProgram, reader))

app = Tk()
app.title("Access Control")
app.geometry('800x610+200+50')
app.protocol('WM_DELETE_WINDOW', closewindow)
updateGUIThread = threading.Thread(target=updateGUI)

app.minsize('800', '610')
app.maxsize('800', '610')

"I'm create Tkinter widget here."

reader = Readers()
settingList = list()
readerType = ""
readerPacket = ""
try:
    for line in fileinput.FileInput("Setting.txt", mode='r'):
        settingList.append(line)
        if str(line).find("DF760MSB", 0, len(str(line))) >= 0:
            readerType = reader._DF760MSB
        elif str(line).find("DF760LSB", 0, len(str(line))) >= 0:
            readerType = reader._DF760LSB
        else:
            readerType = reader._DF760MSB

        if str(line).find("SINGLEPACKET", 0, len(str(line))) >= 0:
            readerPacket = reader.SINGLEPACKET
        elif str(line).find("MULTIPACKET", 0, len(str(line))) >= 0:
            readerPacket = reader.MULTIPACKETS
        else:
           readerPacket = reader.SINGLEPACKET

    ReaderName = str(settingList[0]).rstrip()
    baud = int(settingList[1])
    databit = int(settingList[2])

    HOST = str(settingList[3]).rstrip()
    PORT = int(settingList[4])
    TIMEOUT = int(settingList[5])
except:
    ReaderName = "R001"
    baud = 19200
    databit = 8
    HOST = "10.50.41.81"
    PORT = 43
    TIMEOUT = 10
serialport = 'COM3'
reader.SettingServer(HOST, PORT, TIMEOUT)
app.after(100, functools.partial(initState, reader, ReaderName, readerType, serialport, baud, databit, readerPacket))
app.mainloop()

when I'm run this code GUI will freezing but serial port and TCP client socket still running. I've try to fix this problem (looking in every where) but I'm got nothing. Any idea? Thank so much.

Upvotes: 0

Views: 97

Answers (1)

Benjamin James Drury
Benjamin James Drury

Reputation: 2383

The way to solve this would be to call app.after(100, <methodName>) from the receiving thread. This stops the main thread from being blocked by waiting for a signal, but also means that tkinter can update instantly too as the method pushed to .after will be executed in the main thread. By specifying 100 as the time frame, it will appear to change nigh on instantly as the argument passed is the number of milliseconds.

Upvotes: 2

Related Questions