990 likes | 3.46k Views
( ** Python Certification Training: https://www.edureka.co/python ** ) <br>This Edureka PPT on Tkinter tutorial covers all the basic aspects of creating and making use of your own simple Graphical User Interface (GUI) using Python. It establishes all of the concepts needed to get started with building your own user interfaces while coding in Python.
E N D
Tkinter Agenda Python Certification Training https://www.edureka.co/python
Tkinter Agenda Python Certification Training https://www.edureka.co/python
Agenda 01 Introduction Introduction to Flask 02 Getting Started Installing and working with Flask 03 Concepts Overview of all the concepts in Flask 04 Practical Approach Looking at code to understand theory Python Certification Training https://www.edureka.co/python
Tkinter What Is A Graphical User Interface? Python Certification Training https://www.edureka.co/python
What Is A GUI? GUIis a desktop app which helps you to interact with computers What is GUI? GUI Apps Text Editors Games I’ll learn Tkinter! Python Certification Training https://www.edureka.co/python
Why We Need GUI? Command Line Graphical User Interface The graphical user interface, is a type of user interface that allows users to interact with electronic devices through graphical icons and visual indicators Python Certification Training https://www.edureka.co/python
Tkinter Python Libraries For GUI Python Certification Training https://www.edureka.co/python
Why We Need GUI? Life without Tkinter! Using Tkinter! Python Certification Training https://www.edureka.co/python
Tkinter What Is Tkinter? Python Certification Training https://www.edureka.co/python
Introduction to Flask Tkinterin Python GUI Programming is standard Python GUI library It gives us an object-oriented interface to the Tk GUI toolkit Python Certification Training https://www.edureka.co/python
Tkinter Fundamentals Of Tkinter Python Certification Training https://www.edureka.co/python
First Window Using Tkinter Import the Tkintermodule Create the GUI application main window Enter the main event loop Add Widgets import tkinter window = tkinter.Tk() # to rename the title of the window window.title("GUI") # pack is used to show the object in the window label = tkinter.Label(window, text = "Hello World!").pack() window.mainloop() Python Certification Training https://www.edureka.co/python
Tkinter Tkinter Widgets Python Certification Training https://www.edureka.co/python
Adding Widgets To Our Application A widget is an element of a graphical user interface (GUI) that displays information or provides a specific way for a user to interact with theoperating systemor anapplication Label Button Entry ComboBox CheckButton Radio ScrolledText SpinBox Menu Bar Notebook Python Certification Training https://www.edureka.co/python
Label Widget Label You can set the label font so you can make it bigger and maybe bold Output: Example: l1 = Label (window, text="edureka!“ font=("Arial Bold", 50)) l1.grid (column=0, row=0) Changing the font style and size Python Certification Training https://www.edureka.co/python
Label Widget Label We can set the default window size using geometry function Output: Example: l1 = Label (window, text="edureka!“ font=("Arial Bold", 50)) window.geometry('350x200') l1.grid (column=0, row=0) The above line sets the window width to 350 pixels and the height to 200 pixels Python Certification Training https://www.edureka.co/python
Button Widget Let’s start by adding the button to the window, the button is created and added to the window the same as the label Button Output: Example: bt = Button (window, text="Enter") bt.grid (column=1, row=0) Using grid function to set the button position Python Certification Training https://www.edureka.co/python
Button Widget • You can change foreground for a button or any other widget usingfgproperty. • Also, you can change the background color for any widget usingbgproperty. Button Output: Example: bt = Button (window, text="Enter", bg="orange", fg="red") bt.grid (column=1, row=0) Changing the background and foreground color Python Certification Training https://www.edureka.co/python
Button Widget • Let’s add button click event • First, we will write the function that we need to execute when the button is clicked Button Example: def clicked(): Function that will execute the button click event l1.configure (text="Button was clicked !!") bt = Button (window, text=“Enter”, command=clicked) Wiring the button with the function Python Certification Training https://www.edureka.co/python
Entry Widget In the previous Python GUI examples, we saw how to add simple widgets. Now let’s try getting the user input using TkinterEntry class (Tkintertextbox) Entry Example: txt = Entry(window,width=10) Creating a textbox using TkinterEntry class txt.grid(column=1, row=0) def clicked(): Once the button is clicked show “Welcome to” concatenated with the entered text res = "Welcome to " + txt.get() l1.configure(text= res) bt = Button (window, text=“Enter”, command=clicked) Python Certification Training https://www.edureka.co/python
Entry Widget Entry Python Certification Training https://www.edureka.co/python
Combobox Widget Combobox ComboboxWidgets are very easy to use and are widely used as well! Example: from tkinter.ttk import * Adding the comboboxitems using the tuple combo = Combobox(window) combo['values']= (1, 2, 3, 4, 5, "Text") combo.current(3) Setting the selected item combo.grid(column=0, row=0) Python Certification Training https://www.edureka.co/python
Combobox Widget Python Certification Training https://www.edureka.co/python
Checkbutton Widget Checkbutton To create a checkbuttonwidget, you can use Checkbuttonclass Example: chk_state = BooleanVar() Creating a variable of type BooleanVarwhich is not a standard Python variable, it’s a Tkintervariable chk_state.set (True) chk = Checkbutton(window, text=‘Select', var=chk_state) chk.grid(column=0, row=0) Passing the chk_stateto the Checkbutton class to set the check state Python Certification Training https://www.edureka.co/python
Checkbutton Widget Checkbutton Python Certification Training https://www.edureka.co/python
Radio Button Widget To add radio buttons, simply you can use RadioButtonclass Radio Button Example: rad1 = Radiobutton(window, text=Python', value=1) You should set the value for every radio button with a different value, otherwise, they won’t work rad2 = Radiobutton(window, text=Java', value=2) rad3 = Radiobutton(window, text=Scala', value=3) rad1.grid(column=0, row=0) rad2.grid(column=1, row=0) rad3.grid(column=2, row=0) Python Certification Training https://www.edureka.co/python
Radio Button Widget Radio Button Python Certification Training https://www.edureka.co/python
ScrolledText Widget To add a ScrolledTextwidget, you can use the ScrolledTextclass ScrolledText Example: from tkinter import scrolledtext txt = scrolledtext.ScrolledText(window, width=40,height=10) Here we specify the width and the height of the ScrolledTextwidget, otherwise, it will fill the entire window To set scrolledtextcontent, you can use the insert method -txt.insert(INSERT,'Youtext goes here') Python Certification Training https://www.edureka.co/python
MessageBox Widget To show a message box using Tkinter, you can use messageboxlibrary MessageBox Example: Example: def clicked(): from tkinter import messagebox messagebox.showinfo('Message title', 'Message content') messagebox.showinfo('Message title’, 'Message content') btn = Button(window,text=‘ENTER', command=clicked) Shows a message box when the user clicks a button Python Certification Training https://www.edureka.co/python
MessageBox Widget MessageBox Python Certification Training https://www.edureka.co/python
SpinBox Widget To create a Spinboxwidget, you can use the Spinboxclass SpinBox Example: Output: spin = Spinbox(window, from_=0, to=100, width=5) Python Certification Training https://www.edureka.co/python
Tkinter Geometry Management Python Certification Training https://www.edureka.co/python
Geometry Management All tkinterwidgets will have geometric measurements Geometry Manager Classes pack() grid() place() It organizes the widgets in the block, which mean it occupies the entire available width It organizes the widgets in table-like structure It's used to place the widgets at a specific position you want. Python Certification Training https://www.edureka.co/python
Tkinter Organizing Layout & Widgets Python Certification Training https://www.edureka.co/python
Organizing Layout And Widgets We use Frame class to arrange layout in a window Frame is used to create the divisions in the window. You can align the frames as you like with side parameter of pack() method. Frame Button is used to create a button in the window. It takes several parameters like text(Value of the Button), fg(Colorof the text), bg(Background color) Button Let’s see some code Python Certification Training https://www.edureka.co/python
Organizing Layout And Widgets Sample Login Box Grid is another way to organize the widgets. It uses the Matrix row column concepts. Grid Let’s see more code Python Certification Training https://www.edureka.co/python
Tkinter Binding Functions Python Certification Training https://www.edureka.co/python
Binding Functions Calling functions whenever an event occurs refers to a binding function. Binding import tkinter window = tkinter.Tk() window.title("GUI") # creating a function called say_hi() def say_hi(): tkinter.Label(window, text = "Hi").pack() tkinter.Button(window, text = "Click Me!", command = say_hi).pack() window.mainloop() I like code! Python Certification Training https://www.edureka.co/python
Tkinter Event Handling Python Certification Training https://www.edureka.co/python
Event Handling mousemove, mouseover, clicking, scrolling are some events Events Mouse Buttons <Button-1> <Button-2> <Button-3> Left Click Middle Click Right Click Python Certification Training https://www.edureka.co/python
Tkinter Images & Icons Python Certification Training https://www.edureka.co/python
Images & Icons Images can be added using the PhotoImagemethod Add Images We love Edureka <3 Python Certification Training https://www.edureka.co/python
Tkinter Use Case Python Certification Training https://www.edureka.co/python
Use Case – Simple Calculator Let us design our own basic calculator using tkinter Python Certification Training https://www.edureka.co/python
Tkinter Conclusion Python Certification Training https://www.edureka.co/python
Conclusion I learnt Tkinter, yay! Python Certification Training https://www.edureka.co/python