Our script will remain in the event loop until we close the window.Īs we have already mentioned, labels can contain text and images. The window won't appear until we enter the Tkinter event loop: root.mainloop() The pack method tells Tk to fit the size of the window to the given text. The keyword parameter "text" specifies the text to be shown: w = tk.Label(root, text="Hello Tkinter!") So our Label widget is a child of the root widget. The first parameter of the Label call is the name of the parent window, in our case "root". The next line of code contains the Label widget. The root widget has to be created before any other widgets and there can only be one root widget. To initialize tkinter, we have to create a Tk root widget, which is a window with a title bar and other decoration provided by the window manager. In our example, we imported tkinter by renaming it into tk, which is the preferred way to do it: import tkinter as tk The tkinter module, containing the Tk toolkit, has always to be imported. Under Windows it appears in the Windows look and feel: If you run the command under the Gnome and Linux, the window the window will look like this: If we save the script under the name hello.py, we can start it like this using the command shell: $ python3 hello.py Enjoying this page? We offer live Python training courses covering the content of this site.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |