How to get a tkinter window to display in Linux

PyNEwbie

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 - the tk window is not displayed nor is there a button on the screen 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.

Again, after entering this code nothing visible happens. I deleted the code relating to creating the button and still nothing happens so I am not sure where to start diagnosing the issue I have Googled for information and found nothing useful

Any insight would be appreciated.

I did ask this question on superuser but there is no Tkinter tag so . . .

humm do I need a

tk.pack() 

statement - I will report back.

Bryan Oakley

No, you do not need tk.pack(). What you do need is start the event loop. The event loop, as it's name suggests, is a loop that processes events. Everything in Tkinter happens as a response an event, including the actual drawing of a widget or window on the screen.

As the last line in your file, add the following:

tk.mainloop()

I encourage you to not do the import the way you are doing. I know a lot of tkinter tutorials do it that way, but it's a bad thing to do. Instead, do it like this:

import tkinter as tk
root = tk.Tk()
btn = tk.Button(root, text='click me')
btn.pack()
root.mainloop()

It requires typing three extra characters for every widget, but in exchange you get code that is easier to maintain over time.

PEP8 is the official python style guide, and it explicitly recommends against wildcard imports:

Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).

See http://legacy.python.org/dev/peps/pep-0008/#imports

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get command window output to display in widget with tkinter

From Dev

Get command window output to display in widget with tkinter

From Dev

How to make a window fullscreen in a secondary display with tkinter?

From Dev

How to display a value from children window to parent window in python tkinter?

From Dev

how do i display search results in Python Tkinter window

From Dev

How to display two images on the same Tkinter Toplevel () window

From Dev

How to get the location of the component of a window in Linux?

From Dev

How to get tkinter canvas to dynamically resize to window width?

From Dev

Tkinter: How to get frame in canvas window to expand to the size of the canvas?

From Dev

How to get the HWND of a Tkinter window in python3 on windows?

From Dev

How to clear window with tkinter

From Dev

How do I get a tkinter box to show up in another tkinter window?

From Dev

How can I get a Session in Meteor to display in a separate window?

From Dev

How to get an Xlib.display.Window instance by id?

From Dev

how to display to action of each combo box value in single tkinter window using python

From Dev

Tkinter : How to center the window title

From Dev

How to make a Tkinter window not resizable?

From Dev

How to refresh the GUI window in Tkinter

From Dev

How to check if window is on Fullscreen in Tkinter?

From Dev

how to do a "Save as" window with tkinter?

From Dev

How to get Geometry of currently focused Window on Linux in C++?

From Dev

python tkinter - get cursor in new window

From Dev

Tkinter: Reading int with get() from the window

From Dev

TkInter: how to display the normal cursor?

From Dev

How to get display width of a string from Linux command line?

From Dev

How to get display width of a string from Linux command line?

From Dev

Linux : Get window border height

From Dev

linux open window on desktop without display manager

From Dev

how to a get text from python Tkinter entry after window has closed?

Related Related

  1. 1

    Get command window output to display in widget with tkinter

  2. 2

    Get command window output to display in widget with tkinter

  3. 3

    How to make a window fullscreen in a secondary display with tkinter?

  4. 4

    How to display a value from children window to parent window in python tkinter?

  5. 5

    how do i display search results in Python Tkinter window

  6. 6

    How to display two images on the same Tkinter Toplevel () window

  7. 7

    How to get the location of the component of a window in Linux?

  8. 8

    How to get tkinter canvas to dynamically resize to window width?

  9. 9

    Tkinter: How to get frame in canvas window to expand to the size of the canvas?

  10. 10

    How to get the HWND of a Tkinter window in python3 on windows?

  11. 11

    How to clear window with tkinter

  12. 12

    How do I get a tkinter box to show up in another tkinter window?

  13. 13

    How can I get a Session in Meteor to display in a separate window?

  14. 14

    How to get an Xlib.display.Window instance by id?

  15. 15

    how to display to action of each combo box value in single tkinter window using python

  16. 16

    Tkinter : How to center the window title

  17. 17

    How to make a Tkinter window not resizable?

  18. 18

    How to refresh the GUI window in Tkinter

  19. 19

    How to check if window is on Fullscreen in Tkinter?

  20. 20

    how to do a "Save as" window with tkinter?

  21. 21

    How to get Geometry of currently focused Window on Linux in C++?

  22. 22

    python tkinter - get cursor in new window

  23. 23

    Tkinter: Reading int with get() from the window

  24. 24

    TkInter: how to display the normal cursor?

  25. 25

    How to get display width of a string from Linux command line?

  26. 26

    How to get display width of a string from Linux command line?

  27. 27

    Linux : Get window border height

  28. 28

    linux open window on desktop without display manager

  29. 29

    how to a get text from python Tkinter entry after window has closed?

HotTag

Archive