Tkinter Tutorial Python Pdf Library
An Introduction to Tkinter. Tkinter includes a number of Python modules. Every serious tutorial should start with a “hello world”-type example.
In this tutorial, we will learn how to develop graphical user interfaces by writing some Python GUI examples using the Tkinter package. Tkinter package is shipped with Python as a standard package, so we don't need to install anything to use it. Free download telugu madhu babu shadow new novels fiction. Tkinter is a very powerful package. If you already have installed Python, you may use IDLE which is the integrated IDE that is shipped with Python, this IDE is written using Tkinter.
Sounds Cool!! We will use Python 3.6, so if you are using Python 2.x, it's strongly recommended to switch to Python 3.x unless you know the language changes so you can adjust the code to run without errors. I assume that you have a little background in the to help you understand what we are doing. We will start by creating a window to which we will learn how to add widgets such as buttons, combo boxes, etc. Then we will play with their properties, so let's get started.
Create Your First GUI Application First, we will import THE Tkinter package and create a window and set its title: from tkinter import * window = Tk() window.title('Welcome to LikeGeeks app') window.mainloop() The result will look like this: Awesome! Our application works. The last line calls the mainloop function.
This function calls the endless loop of the window, so the window will wait for any user interaction till we close it. If you forget to call the mainloop function, nothing will appear to the user. Create a Label Widget To add a label to our previous example, we will create a label using the label class like this: lbl = Label(window, text='Hello') Then we will set its position on the form using the grid function and give it the location like this: lbl.grid(column=0, row=0) So the complete code will be like this: from tkinter import * window = Tk() window.title('Welcome to LikeGeeks app') lbl = Label(window, text='Hello') lbl.grid(column=0, row=0) window.mainloop() And this is the result: Without calling the grid function for the label, it won't show up. Set Label Font Size You can set the label font so you can make it bigger and maybe bold. You can also change the font style. To do so, you can pass the font parameter like this: lbl = Label(window, text='Hello', font=('Arial Bold', 50)) Note that the font parameter can be passed to any widget to change its font, thus it applies to more than just labels.
Great, but the window is so small, what about setting the window size? Setting Window Size We can set the default window size using the geometry function like this: window.geometry('350x200') The above line sets the window width to 350 pixels and the height to 200 pixels. Let's try adding more GUI widgets like buttons and see how to handle button click events. Parallels desktop 7 activation key serial. Adding a Button Widget Let's start by adding the button to the window. The button is created and added to the window in the same way as the label: btn = Button(window, text='Click Me') btn.grid(column=1, row=0) So our window will be like this: from tkinter import * window = Tk() window.title('Welcome to LikeGeeks app') window.geometry('350x200') lbl = Label(window, text='Hello') lbl.grid(column=0, row=0) btn = Button(window, text='Click Me') btn.grid(column=1, row=0) window.mainloop() The result looks like this: Note that we place the button on the second column of the window, which is 1. If you forget and place the button on the same column which is 0, it will show the button only, since the button will be on the top of the label.
Change Button Foreground and Background Colors You can change the foreground of a button or any other widget using the fg property. Also, you can change the background color of any widget using the bg property. Btn = Button(window, text='Click Me', bg='orange', fg='red') Now, if you tried to click on the button, nothing happens because the click event of the button isn't written yet. Handle Button Click Event First, we will write the function that we need to execute when the button is clicked: def clicked(): lbl.configure(text='Button was clicked!!' ) Then we will wire it with the button by specifying the function like this: btn = Button(window, text= 'Click Me', command=clicked) Note that, we typed clicked only not clicked() with parentheses. Now the full code will be like this: from tkinter import * window = Tk() window.title('Welcome to LikeGeeks app') window.geometry('350x200') lbl = Label(window, text='Hello') lbl.grid(column=0, row=0) def clicked(): lbl.configure(text='Button was clicked!!' ) btn = Button(window, text='Click Me', command=clicked) btn.grid(column=1, row=0) window.mainloop() And when we click the button, the result, as expected, looks like this: Cool!