PyNEwbie
PyNEwbie

Reputation:

How can I get a tkinter window to display in LINUX

I am trying to follow along in the book Python Programming for Kids. I am working with a group of neighborhood kids and to reduce the cost we are using the Raspberry Pi as our computer. I am a Windows guy and the GUI builder of choice for me is WxPython. I am trying to get ready for next weeks class and have run into a problem. I have entered the code below

from tkinter import *
tk = Tk()
btn = Button(tk,text = 'click me')
btn.pack()

according to the book the second line is supposed to create a window (frame I think in the Wx world) and the third line defines a button object and the fourth inserts it in the window.

However, this is not working and I have not been able to figure out why. tkinter is imported and the tk object has lots of methods/properties visible when I type dir(tk) so I know that we have tkinter on the Pi's.

Any insight would be appreciated.

Upvotes: 0

Views: 270

Answers (1)

patthoyts
patthoyts

Reputation: 33193

You have to run the windows system event loop and process events. This means the last command in your program should be tk.mainloop(). The X Windows System operated in a similar manner to Windows. The system dispatches event messages whenever something happens like the mouse moving, a button being clicked or a window needs redrawing and so on. On Windows you would have to 'pump the message queue' using GetMessage() and DispatchMessage(). With Tkinter this is handled in the mainloop() function (for both Windows and X).

Upvotes: 2

Related Questions