0

I have a class GUI, where I set up all my widgets etc. for my GUI. I use threading to start a process from another class. This works fine, as long that other process just runs through. In some cases, I need to wait for a user input to proceed. I used tkmessagebox for this, but the messagebox doesn't appear and blocks the GUI without any error message. (It works when not started through the GUI).

Here's part of my code:

GUI part

from Tkinter import *
import Tkinter as ttk
import ttk
from tkMessageBox import *
from GUI_reader import Commandline_Reader
import threading
import Queue

class GUI:
    def __init__(self,master):
        self.master = master
        self.argString='ds'
        ## self.workerThread()
        button = ttk.Button(text='start', command=self.go).grid()
        ...
    def go(self):
        self.thread = threading.Thread(target=self.workerThread)
        self.thread.daemon = True
        self.thread.start()
        ...
    def workerThread(self):
        ...
        reader = Commandline_Reader(master, self.argString)
        if reader.connect():
            print 'success'
            reader.run()
            print 'success'

if __name__ == '__main__':
    root = Tk()
    client = GUI(root)
    root.mainloop()

class commandline_reader:

import tkMessageBox
...

class Commandline_Reader:

    def __init__(self, master, argString='')
        self.master = master
        ...

    def connect(self)
        ...

    def run(self):
        ...
        tkMessageBox.askokcancel('Calibration', 'Hit ok to start calibration', parent= self.master)
        ...

if __name__ == '__main__':
    reader = Commandline_Reader(self,master)
    if not reader.connect():
        exit(-1)
    if not reader.run():
        exit(-2)
  • when the _messagebox doesn't appear_ , how does it _block_ the GUI? & what do you mean by _block_ here? – Alok Mar 25 '14 at 11:08
  • the process from Commandline_Reader is interrupted at the point where the messagebox should appear and the Main GUI window is frozen, as if its waiting for something.. – user3197620 Mar 25 '14 at 11:12
  • try printing out things to see if it is even getting to the `run()` method – Alok Mar 25 '14 at 11:16
  • Yes it is, I printed something just before the messagebox command. – user3197620 Mar 25 '14 at 11:19
  • I was using dummy values & its working fine on my system...is there any error that you are getting? – Alok Mar 25 '14 at 11:38
  • No, no error! But when I print self.master in the run() method, it prints an empty string. Do I need to give the master in each method and not only in init?? Ah no, self.master cannot be printed this way i guess... – user3197620 Mar 25 '14 at 11:47
  • & why would you print `self.master`? just print any string like "hello" "success" or anything like that – Alok Mar 25 '14 at 11:50
  • ;-) you're right. But anyway, i don't get any error... – user3197620 Mar 25 '14 at 11:51
  • ok [this](http://pastebin.com/3irJA2yU) & [this](http://pastebin.com/bQnMs9BJ) is what i tried & it worked perfectly – Alok Mar 25 '14 at 11:54
  • That works also on my system... I'll try to figure out what else might bother it. Thanks for your help! – user3197620 Mar 25 '14 at 12:03
  • Ok the problem seems to be that in the GUI class I import: – user3197620 Mar 25 '14 at 14:47
  • So now is it working fine? – Alok Mar 25 '14 at 14:49
  • Tkinter import *, import Tkinter as ttk, import ttk. The GUI only works when imported in this order, while the tkMessageBox only appears to work when imported as: import ttk, from Tkinter import *, import Tkinter as ttk. Any suggestions how to fix this? – user3197620 Mar 25 '14 at 14:55
  • just do `from Tkinter import Tk` & `import ttk` . i think that should work just fine – Alok Mar 25 '14 at 15:02
  • 'from Tkinter import *, import Tkinter as ttk, import ttk works (with messagebox) when I start the worker thread from GUI __init__ (self.workerThread()) but not when started in def go() with self.thread = threading.Thread(target= self.workerThread) and self.thread.start() – user3197620 Mar 25 '14 at 15:13
  • but in this case, the run() method starts without the gui appearing – user3197620 Mar 25 '14 at 15:19
  • can you update your question with this? its too hard to understand here in this comment – Alok Mar 25 '14 at 15:21
  • i was running it in debug mode & it was saying _waiting for network_ . I'm sorry but I'm not able to get it :| – Alok Mar 26 '14 at 04:05
  • 1
    Thanks for your effort. I found: http://stackoverflow.com/questions/7014984/tkinter-tkmessagebox-not-working-in-thread, says it's because tkinter is not threadsafe. I will try build my own messagebox with a label and two buttons, maybe it doesn't get stuck that way. – user3197620 Mar 26 '14 at 08:37

0 Answers0