{"id":2365,"date":"2023-10-21T08:04:12","date_gmt":"2023-10-21T02:34:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=2365"},"modified":"2023-11-10T11:53:09","modified_gmt":"2023-11-10T06:23:09","slug":"python-gui-programming-with-tkinter","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-gui-programming-with-tkinter\/","title":{"rendered":"Python GUI Programming With Tkinter"},"content":{"rendered":"
Graphical User Interface (GUI)\u00a0is nothing but a desktop application which helps you to interact with the computers. They perform different tasks in the desktops, laptops and other electronic devices.<\/p>\n
They are some different types of\u00a0GUI\u00a0apps which we daily use on the laptops or desktops. We are going to learn how to create those type of apps.<\/p>\n
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy task.<\/p>\n
Tkinter\u00a0is actually an inbuilt\u00a0Python\u00a0module used to create simple\u00a0GUI\u00a0apps. It is the most commonly used module for\u00a0GUI\u00a0apps in the\u00a0Python.<\/p>\n
You don\u2019t need to worry about installation of the\u00a0Tkinter\u00a0module as it comes with\u00a0Python\u00a0default.<\/p>\n
Importing tkinter is same as importing any other module in the Python code. Note that the name of the module in Python 2.x is \u2018Tkinter\u2019 and in Python 3.x it is \u2018tkinter\u2019.<\/p>\n
import tkinter<\/pre>\nThere are two main methods used which the user needs to remember while creating the Python application with GUI.<\/p>\n
1.Tk(screenName=None, \u00a0baseName=None, \u00a0className=\u2019Tk\u2019, \u00a0useTk=1):<\/h3>\n
To create a main window, tkinter offers a method \u2018Tk(screenName=None, \u00a0baseName=None, \u00a0className=\u2019Tk\u2019, \u00a0useTk=1)\u2019. To change the name of the window, you can change the className to the desired one. The basic code used to create the main window of the application is:<\/p>\n
m=tkinter.Tk() where m is the name of the main window object<\/pre>\n2.mainloop():<\/h3>\n
There is a method known by the name mainloop() is used when your application is ready to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event as long as the window is not closed.<\/p>\n
m.mainloop()<\/pre>\n
\nOutput:<\/h3>\n<\/p>\n
tkinter also offers access to the geometric configuration of the widgets which can organize the widgets in the parent windows. There are mainly three geometry manager classes class.<\/p>\n
\n
- pack() method:It organizes the widgets in blocks before placing in the parent widget.<\/li>\n
- grid() method:It organizes the widgets in grid (table-like structure) before placing in the parent widget.<\/li>\n
- place() method:It organizes the widgets by placing them on specific positions directed by the programmer.<\/li>\n<\/ol>\n
There are a number of widgets which you can put in your tkinter application. Some of the major widgets are explained below:<\/p>\n
1.Button:<\/h3>\n
To add a button in your application, this widget is used.
\nThe general syntax is:<\/p>\nw=Button(master, option=value)<\/pre>\nmaster is the parameter used to represent the parent window.
\nThere are number of options which are used to change the format of the Buttons. Number of options can be passed as parameters separated by commas. Some of them are listed below.<\/p>\n\n
- activebackground: to set the background color when button is under the cursor.<\/li>\n
- activeforeground: to set the foreground color when button is under the cursor.<\/li>\n
- bg: to set he normal background color.<\/li>\n
- command: to call a function.<\/li>\n
- font: to set the font on the button label.<\/li>\n
- image: to set the image on the button.<\/li>\n
- width: to set the width of the button.<\/li>\n
- height: to set the height of the button.<\/li>\n<\/ul>\n
\nOutput:<\/h3>\n
\n2.Canvas:<\/h3>\nIt is used to draw pictures and other complex layout like graphics, text and widgets.
\nThe general syntax is:<\/p>\nw = Canvas(master, option=value)\r\nmaster is the parameter used to represent the parent window.<\/pre>\nThere are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.<\/p>\n
\n
- bd<\/b>: to set the border width in pixels.<\/li>\n
- bg<\/b>: to set the normal background color.<\/li>\n
- cursor<\/b>: to set the cursor used in the canvas.<\/li>\n
- highlightcolor<\/b>: to set the color shown in the focus highlight.<\/li>\n
- width<\/b>: to set the width of the widget.<\/li>\n
- height<\/b>: to set the height of the widget.<\/li>\n<\/ul>\n
\nOutput:<\/h3>\n
\n3.CheckButton:<\/h3>\n<\/b>To select any number of options by displaying a number of options to a user as toggle buttons. The general syntax is:<\/p>\n
w = CheckButton(master, option=value)<\/pre>\nThere are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.<\/p>\n
\n
- Title: To set the title of the widget.<\/li>\n
- activebackground: to set the background color when widget is under the cursor.<\/li>\n
- activeforeground: to set the foreground color when widget is under the cursor.<\/li>\n
- bg: to set he normal backgrouSteganographyBreakSecret Code:Attach a File:nd color.<\/li>\n
- command: to call a function.<\/li>\n
- font: to set the font on the button label.<\/li>\n
- image: to set the image on the widget.<\/li>\n<\/ul>\n
\n\n