C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python Tkinter TutorialTkinter tutorial provides basic and advanced concepts of Python Tkinter. Our Tkinter tutorial is designed for beginners and professionals. Python provides the standard library Tkinter for creating the graphical user interface for desktop based applications. Developing desktop based applications with python Tkinter is not a complex task. An empty Tkinter top-level window can be created by using the following steps.
Example# !/usr/bin/python3 from tkinter import * #creating the application main window. top = Tk() #Entering the event main loop top.mainloop() Output: Tkinter widgetsThere are various widgets like button, canvas, checkbutton, entry, etc. that are used to build the python GUI applications.
Python Tkinter GeometryThe Tkinter geometry specifies the method by using which, the widgets are represented on display. The python Tkinter provides the following geometry methods.
Let's discuss each one of them in detail. Python Tkinter pack() methodThe pack() widget is used to organize widget in the block. The positions widgets added to the python application using the pack() method can be controlled by using the various options specified in the method call. However, the controls are less and widgets are generally added in the less organized manner. The syntax to use the pack() is given below. syntaxwidget.pack(options) A list of possible options that can be passed in pack() is given below.
Example# !/usr/bin/python3 from tkinter import * parent = Tk() redbutton = Button(parent, text = "Red", fg = "red") redbutton.pack( side = LEFT) greenbutton = Button(parent, text = "Black", fg = "black") greenbutton.pack( side = RIGHT ) bluebutton = Button(parent, text = "Blue", fg = "blue") bluebutton.pack( side = TOP ) blackbutton = Button(parent, text = "Green", fg = "red") blackbutton.pack( side = BOTTOM) parent.mainloop() Output: Python Tkinter grid() methodThe grid() geometry manager organizes the widgets in the tabular form. We can specify the rows and columns as the options in the method call. We can also specify the column span (width) or rowspan(height) of a widget. This is a more organized way to place the widgets to the python application. The syntax to use the grid() is given below. Syntaxwidget.grid(options) A list of possible options that can be passed inside the grid() method is given below.
Example# !/usr/bin/python3 from tkinter import * parent = Tk() name = Label(parent,text = "Name").grid(row = 0, column = 0) e1 = Entry(parent).grid(row = 0, column = 1) password = Label(parent,text = "Password").grid(row = 1, column = 0) e2 = Entry(parent).grid(row = 1, column = 1) submit = Button(parent, text = "Submit").grid(row = 4, column = 0) parent.mainloop() Output: Python Tkinter place() methodThe place() geometry manager organizes the widgets to the specific x and y coordinates. Syntaxwidget.place(options) A list of possible options is given below.
Example# !/usr/bin/python3 from tkinter import * top = Tk() top.geometry("400x250") name = Label(top, text = "Name").place(x = 30,y = 50) email = Label(top, text = "Email").place(x = 30, y = 90) password = Label(top, text = "Password").place(x = 30, y = 130) e1 = Entry(top).place(x = 80, y = 50) e2 = Entry(top).place(x = 80, y = 90) e3 = Entry(top).place(x = 95, y = 130) top.mainloop() Output: PrerequisiteBefore learning Tkinter, you must have the basic knowledge of Python. AudienceOur Python Tkinter tutorial is designed to help beginners and professionals. ProblemWe assure that you will not find any problem in this Tkinter tutorial. But if there is any mistake, please post the problem in contact form.
Next TopicPython Tkinter Button
|