Tkinter Tutorial Python Pdf Library

admin

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. 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.

• Tkinter is Python’s default GUI library. It is based on the Tk toolkit, originally designed for the Tool Command Language (Tcl). Due to Tk’s popularity, it has been ported to a variety of other scripting languages, including Perl (Perl/Tk), Ruby (Ruby/Tk), and Python (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!

Tkinter Tutorial Python Pdf Library

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. 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.

“Volume’s been a little light, but we’re not in a perfect market right now.” Redler said he expects markets to rise further, led by technology stocks such as Apple, IBM and Intel. Kotok am sigish. “During this entire rally, you’ve had the calculated pull-ins, you’ve had leadership and rotation,” he said.

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.