2

I'm relatively new to Python and especially new to tkinter. In the following example code, the dialog works fine when created directly (left button), but the application becomes unresponsive if the dialog is created from a thread (right button). What seems to be the problem? Can threads launch dialogs? If so, how?

from tkinter import *
from tkinter import ttk
import threading 

count = 0

def makeDialog():
    global count
    count = count + 1;
    messagebox.showinfo('Click Counter','Click #{0}'.format(count))

def makeThread():
    threading.Thread(target=makeDialog).start()

root = Tk()
root.title("Dialogs")
mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0)
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
button1 = ttk.Button(mainframe, text="Dialog", command=makeDialog)
button1.grid(row=1, column=1)
button2 = ttk.Button(mainframe, text="Thread Dialog", command=makeThread)
button2.grid(row=1, column=2)
root.mainloop()
ProfPlum
  • 129
  • 1
  • 8
  • See Alex Martelli's [answer to a similar question](https://stackoverflow.com/questions/3567238/i-need-a-little-help-with-python-tkinter-and-threading). – Ned Deily Mar 29 '11 at 02:38
  • Thanks, Ned. Somehow I missed this question in my initial searches. It does address my problem directly. Thanks! – ProfPlum Mar 29 '11 at 10:30
  • Possible duplicate of [Threaded Tkinter script crashes when creating the second Toplevel widget](https://stackoverflow.com/questions/3567238/threaded-tkinter-script-crashes-when-creating-the-second-toplevel-widget) – stovfl Oct 29 '19 at 20:23

0 Answers0