0%
Loading ...

Python Tkinter

This tutorial category contains all the tutorials related to Python Tkinter.

Python Tkinter MenuButton

In this tutorial, we will learn about Python Tkinter MenuButton. Tkinter MenuButton is a widget which we use to show the drop-down menu on the screen. We use a Menu widget with this MenuButton to show options to the user to select from. Syntax to add Python Tkinter MenuButton w = Menubutton (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example Program of Python Tkinter MenuButton from tkinter import * mainWindow = Tk() mainWindow.geometry(“300×150″) menuButton= Menubutton (mainWindow, text=”Select Language”, relief=RAISED ) menuButton.grid() menuButton.menu = Menu ( menuButton, tearoff = 0 ) menuButton[“menu”] = menuButton.menu pythonVar = IntVar() javaVar = IntVar() menuButton.menu.add_checkbutton ( label=”Python”, variable=pythonVar ) menuButton.menu.add_checkbutton ( label=”Java”, variable=javaVar ) menuButton.pack() mainWindow.mainloop() Various possible options in Python Tkinter MenuButton: Option Description activebackground This option is used to set background color of widget under focus. activeforeground This option is used to set foreground color of widget under focus. anchor This option helps us to set position of text inside the widget. By default value of anchor is CENTER. bg This option helps us to set normal background color of widget. bd This option helps us to set the border size around widget. cursor This option helps us to set the style of cursor like an arrow, dot etc. direction This option is used to set the position of menu to display such as LEFT, RIGHT, or ABOVE.  disabledforeground This option is used to disable text color of widget. fg This option helps us to set normal foreground colour of widget. height This option helps us to set height of widget. highlightcolor This option is used to set highlight color shown to the widget under focus. justify This helps us to automatically organize the text in multiple lines. menu This option is used to set menu with MenuButton. padx This option helps us to set space left and right of widget. pady This option helps us to set space above and below of widget. relief This helps us to set the style of the border by which is default Flat. state This option is used to set state of MenuButton such as ENABLE or DISABLE. text This option helps us to set string source for label. underline This option helps us to underline specified letter of the text. width This helps us to set the width of the widget in characters. By default it is 20. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter MenuButton Read More »

Python Tkinter ListBox

In this tutorial, we will learn about Python Tkinter ListBox. Tkinter ListBox helps up to show a list of items in application and from this list, user can select multiple options. Syntax to add Python Tkinter ListBox: w = ListBox (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example Program: from tkinter import * mainWindow = Tk() mainWindow.geometry(“300×150″) listBox = Listbox(mainWindow, height=”5”) listBox.insert(1, “Android”) listBox.insert(2, “C”) listBox.insert(3, “Java”) listBox.insert(4, “PHP”) listBox.insert(5, “Python”) listBox.insert(6, “Ruby”) listBox.pack() mainWindow.mainloop() Various possible options in Python Tkinter ListBox: Option Description bg This option helps us to set normal background color of widget. bd This option helps us to set the border size around widget. cursor This option helps us to set the style of cursor like an arrow, dot etc font This option helps us to set the style of font. fg This option helps us to set normal foreground colour of widget. height This option helps us to set height of widget. highlightcolor This option is used to set color of list item at time of widget in focus. highlightthickness This option is used to set the thickness of highlight. relief This helps us to set the style of the border by which is default Flat. selectbackground This option is used to set background color for selected text. selectmode This options helps us to set selectmode of options means how many item a user can select in single time.  This option can be set to BROWSE, SINGLE, MULTIPLE, EXTENDED. width This helps us to set the width of the widget. xscrollcommand This option used to let the user scroll the Listbox horizontally. yscrollcommand This option used to let the user scroll the Listbox vertically. Python Tkinter ListBox Methods: After learning about various available options in Python Tkinter ListBox. Its time to check out some available methods for Python Tkinter ListBox widget. METHOD DESCRIPTION activate ( index ) This method helps us to select lines at the specified index in the method. curselection() This method returns a tuple with numbers of selected lines in ListBox. In case of nothing selected it will return an empty tuple. delete(first, last = None) This method is used to delete lines between the mentioned index range. get(first, last = None) This method is used to get all the items with falls between the mentioned index range. index(i) This method is used to place a line from the specified index at the top of the widget. insert(index, *elements) This method is used to add new lines before the specified index. see(index) This method is used to adjust the position for ListBox to make sure the line at given index is visible. size() This method returns the total number of lines in the ListBox widget. xview() This method is used to make ListBox widget horizontally scrollable. yview() This method is used to make the ListBox widget vertically scrollable. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter ListBox Read More »

Python Tkinter LabelFrame

In this tutorial, we will learn about Python Tkinter LabelFrame. Tkinter LabelFrame acts as a container and used to create a border around child widgets. LabelFrame widget has all the features of the frame. The thing which makes LabelFrame different from normal Frame widget is, it can show label as well. Syntax to add Python Tkinter LabelFrame: w = LabelFrame( master, option) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example Program of Python Tkinter LabelFrame: from tkinter import * mainWindow = Tk() mainWindow.geometry(“300×200″) labelframe = LabelFrame(mainWindow, text=”This is Label Frame”) labelframe.pack(fill=”both”, expand=”yes”) label = Label(labelframe, text=”This is Label inside in a LabelFrame”) label.pack() mainWindow.mainloop() Various possible options in Python Tkinter LabelFrame: Check out various available options in Python Tkinter LabelFrame. OPTION DESCRIPTION bg This option is used to set the background colour of the widget. bd This option is used to set the size of the border around the indicator. By default, it is 2 px. container This option is used to make a LabelWidget act as a container. Set container=true to make LabelWidget a container. By default, it is false. cursor This option used to set the style of cursor like an arrow, dot etc fg This option is used to set the foreground colour of the widget. font This option is used to set the font style in the widget. height This option is used to set the height of the widget. labelAnchor This option is used to set the position of the label in the widget. By default, the position is NW(north-west). labelWidget This option is used to set the widget for the label. By default, LabelFrame uses text as the label. highlightbackground This option is used to set the focus highlight border when the widget doesn’t have the focus. highlightcolor This option is used to set the colour of the highlight. highlightthickness This option is used to set the width of the highlight border. padx This option is used to set the horizontal padding of the widget. pady This option is used to set vertical padding of the widget. relief This helps us to set the style of the border by which is default GROOVE. text This option is used to set the text for the LabelFrame. width This option is used to set the width of the widget(LabelFrame). Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter LabelFrame Read More »

Python Tkinter Label

In this tutorial, we will learn about the Python Tkinter Label. Tkinter Label is used to provide a caption to widgets in Python application. Tkinter Label can be used to display normal text and images. Syntax of Python Tkinter Label: w = Label (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example of Python Tkinter Label: from tkinter import * top = Tk() top.geometry(“400×200”) username = Label(top, text = “UserName”).place(x = 30,y = 50) password = Label(top, text = “Password”).place(x = 30, y = 90) sbmitbtn = Button(top, text = “Login”,activebackground = “skyblue”, activeforeground = “white”).place(x = 30, y = 130) usernameE = Entry(top).place(x = 100, y = 50) passwordE = Entry(top).place(x = 100, y = 90) top.mainloop() Various possible options in Tkinter Label: Option Description anchor This option helps us to set position of text inside the widget. By default value of anchor is CENTER. bg This option helps us to set normal background color of widget. bitmap This option helps us to display an image using label. You just have to set this option equal to an image object. bd This option helps us to set the border size around widget. cursor This option helps us to set the style of cursor like an arrow, dot etc font This option helps us to set the style of font. fg This option helps us to set normal foreground colour of widget. height This option helps us to set height of widget. justify This helps us to automatically organize the text in multiple lines. padx This option helps us to set space left and right of widget. pady This option helps us to set space above and below of widget. relief This helps us to set the style of the border by which is default Flat. text This option helps us to set string source for label. underline This option helps us to underline specified letter of the text. width This helps us to set the width of the widget. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter Label Read More »

Python Tkinter Frame

In this tutorial, we will learn about Python Tkinter Frame. Tkinter Frame widget is used for grouping of other Tkinter Widgets. It works as a container to other Widgets like Button, Entry etc. Frame widget occupies a rectangular area on the screen. Syntax to add Python Tkinter Frame: w = Frame (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example: from tkinter import * mainWindow = Tk() mainWindow.geometry(“300×150″) frame = Frame(mainWindow) frame.pack() bottomframe = Frame(mainWindow) bottomframe.pack( side = BOTTOM ) button1 = Button(frame, text=”Button 1″, bg=”red”, fg=”white”) button1.pack( side = LEFT) button2 = Button(frame, text=”Button 2″, bg=”blue”, fg=”white”) button2.pack( side = LEFT ) button3 = Button(frame, text=”Button 3″, bg=”green”, fg=”white”) button3.pack( side = LEFT ) button4 = Button(bottomframe, text=”Button 4″, bg=”yellow”, fg=”black”) button4.pack( side = BOTTOM) mainWindow.mainloop() Various possible options in Python Tkinter Frame: Option Description bg This option helps us to set background colour displayed behind the widget. bd This option helps us to set the width of the border around the widget. By default size of the border is 2px. cursor This option helps us to set the style of cursor like an arrow, dot etc height This option helps us to set the height of Frame. width This option helps us to set the width of Frame. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter Frame Read More »

Python Tkinter Entry

In this tutorial, we will learn about Python Tkinter Entry. Tkinter Entry widget is used to get line entry from the user. We cannot get multiple line entry using the Python Tkinter Entry widget. There is a separate widget to get multiple line entry called Text widget. Syntax to add Python Tkinter Entry: w = Entry (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example Program: from tkinter import * top = Tk() top.geometry(“400×200”) username = Label(top, text = “UserName”).place(x = 30,y = 50) password = Label(top, text = “Password”).place(x = 30, y = 90) sbmitbtn = Button(top, text = “Login”,activebackground = “skyblue”, activeforeground = “white”).place(x = 30, y = 130) usernameE = Entry(top).place(x = 100, y = 50) passwordE = Entry(top).place(x = 100, y = 90) top.mainloop() Various possible options in Python Tkinter Entry: Option Description bg This option helps us to set background colour displayed behind the widget. bd This option helps us to set the width of the border around the widget. By default size of the border is 2px. cursor This option helps us to set the style of cursor like an arrow, dot etc exportselection Text of entry widget automatically copied to clipboard. You can stop it from coping by setting exportsection to 0. fg This option helps us to set the color of the text in the entry widget. font This option helps us to set the style of font. justify This helps us to automatically organize the text in multiple lines. width This helps us to set the width of the widget. relief This helps us to set the style of the border by which is default Flat. show This helps us to show text inside an entry widget in some other type. for example, in case of the password, we show (*). Python Tkinter Entry Methods: After learning about various available options in Python Tkinter Entry. Its time to check out some available methods for Python Tkinter Entry widget. METHODS DESCRIPTION get() This method helps us to get a user-entered text from the entry widget. delete ( first, last=None ) This method helps us to delete specified characters from the text in the entry. icursor(index) This method helps us to set the position of the insertion cursor. index(index) This method helps us to set the position of the cursor on the left side of character at the specified index number. insert(index,s) This method helps us to add the specified string before the character at the specified position. select_clear() This method helps us to deselect the selection if something is selected. select_present() This method will return true if something is selected in the entry widget otherwise false. select_range(start,end) This method helps us to select text in the entry between the specified index range. select_to(index) This method helps us to select all the characters from the beginning to the specified index. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter Entry Read More »

Python Tkinter CheckButton

In this tutorial, we will learn about Python Tkinter CheckButton. The Python Tkinter CheckButtons are used to show options to the user to choose. We can add any number of CheckButtons according to the requirement of the application. The important thing to note about checkbox is the user can choose multiple checkboxes from given options. If you want to user choose the only single option you can use the radio button. Syntax to add Python Tkinter CheckButton: w = Checkbutton ( master, option, … ) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example Program: from tkinter import * mainWindow = Tk() #setting size of main window mainWindow.geometry(“200×200”) cb1 = Checkbutton(mainWindow, text = “Python”, \ onvalue = 1, offvalue = 0, height=5, \ width = 20) cb2 = Checkbutton(mainWindow, text = “Java”, \ onvalue = 1, offvalue = 0, height=5, \ width = 20) cb1.pack() cb2.pack() mainWindow.mainloop() Various possible options in Python Tkinter CheckButton: Option Description activebackground This option helps us to set background color of widget when on cursor hover. activeforeground This option helps us to set foreground color of widget when on cursor hover. bg This option helps us to set normal background color of label and indicator. bd This option helps us to set the border size around indicator. fg This option helps us to set normal foreground colour of widget. height This option helps us to set height of widget. command This option helps us to mention a function to every time when checkbutton will change its state. cursor This option helps us to set the style of cursor like an arrow, dot etc font This option helps us to set the style of font. justify This helps us to automatically organize the text in multiple lines. padx This option helps us to set space left and right between text and checkbox. pady This option helps us to set space above and below. relief This helps us to set the style of the border by which is default Flat. width This helps us to set the width of the widget. Python Tkinter CheckButton Methods: After learning about various available options in Python Tkinter CheckButton. Its time to check out some available methods for Python Tkinter CheckButton widget. METHOD DESCRIPTION deselect() This method called to deselect the CheckButton. flash() This method is used to flash CheckButton between normal and active state. invoke() This method helps us to invoke the associated method with CheckButton. select() This method is used to select the CheckButton. toggle() This method helps us to toggle between different CheckButton. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter CheckButton Read More »

Python Tkinter Canvas

In this tutorial, we will learn about Python Tkinter Canvas. Python Tkinter Canvas allows us to add graphics in our Python application. With the help of Tkinter Canvas, we can draw lines, circles, add images and other widgets. Furthermore, we can draw graphs and plots with the help of Tkinter Canvas. Let’s start with a small example Here we will try to draw a line in =Tkinter Canvas. We will provide a total of four coordinates mean a total of four integer values like this(x1,y1) and (x2,y2). Syntax to add Python Tkinter Canvas: w = Canvas (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example to Draw a line in Tkinter Canvas from tkinter import * mainWindow = Tk() #Setting height and width of canvas canvasWindow = Canvas(mainWindow, width=200, height=200) canvasWindow.pack() #Getting mid of canvas vertically screenMid = int(200 / 2) # creating line in cavas using create_line method canvasWindow.create_line(0, screenMid, 200, screenMid, fill=”#333″) mainloop() Here you can see how we can create a simple line using create_line method in Tkinter Canvas. Here we draw a horizontal line. I will recommend you to draw a vertical line by yourself. It will help you to understand this create_line method. Let’s move to the next example. In the next example, we will learn how we can create a rectangle in our Tkinter Canvas. Example to Draw a rectangle in Python Tkinter Canvas from tkinter import * mainWindow = Tk() #Setting height and width of canvas canvasWindow = Canvas(mainWindow, width=300, height=200) canvasWindow.pack() # creating line in cavas using create_line method canvasWindow.create_rectangle(50, 50, 250, 150, fill=”red”) mainloop() Various Possible Options in Python Tkinter Canvas Option Description bd This option we can use to set border width of the canvas in pixels. By default width is 2px. bg This option we can use to set the background colour of the canvas. confine This option we can use to make canvas un scrollable. height This option we can use to set the height of the canvas. relief This option we can use to set border type. The possible types are SUNKEN, RAISED, GROOVE, and RIDGE. width This option we can use to set width of canvas. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter Canvas Read More »

Python Tkinter Button

In this tutorial, we will learn about Python Tkinter Button. This Python Tkinter widget is used to add different kinds of buttons in our Python application. You can set configurations of the button according to the requirements of your application. Furthermore, we can set functionality to our button that what will happen when this button will get pressed. In this tutorial, we will see different Tkinter button options that how we can customize our Button. Syntax to add Python Tkinter Button: w = Button (master, options) master: This represents the parent window. options: Here is the list of most commonly used options for this widget. Example program of Tkinter button: Without wasting time I would like to start with this example program that how we can add Python Tkinter Button in our application. from tkinter import * #setting up main window mainWindow = Tk() #setting size of main window mainWindow.geometry(“200×100”) #Creating new button myButton = Button(mainWindow,text = “Submit”) myButton.pack() mainWindow.mainloop() Example program of Button click action in Python: In this example program, we will learn how we can perform some action when our button will get clicked. from tkinter import * from tkinter import messagebox #setting up main window mainWindow = Tk() #setting size of main window mainWindow.geometry(“200×100”) # defining a function def show(): messagebox.showinfo(“Info”, “Hello, You Clicked this Button”) #Creating new button myButton = Button(mainWindow,command = show, text = “Submit”) myButton.pack() mainWindow.mainloop() Various possible options in Tkinter button: Option Description activebackground This option helps us to set background color of button on mouse hover. activeforeground This option helps us to set font color of button on mouse hover. Bd This option helps us to set border width in pixel. Bg This option helps us to set the background colour button. Fg This option helps us to set foreground color button. Font This option helps us to set font of button. Height This option helps us to set height of button. Highlightcolor This option helps us to set Highlight color when button will get focus. Image This option helps us to set image displayed on button. Padx This option helps us to set horizontal padding in button. Pady This option helps us to set vertical padding in button. Relief This option helps us to set type of border on button like SUNKEN, RAISED, GROOVE, and RIDGE.. State This option helps us to set button disable. Underline This option helps us to set button text underline. Width This option helps us to set width of button Tkinter Button Methods: After learning about various available options in Python Tkinter Button. Its time to check out some available methods for Tkinter Button widget. METHOD DESCRIPTION flash() This method helps us to flash widget many times between active and normal colours. invoke() This method helps us to invoke the associated method with Python Tkinter Button. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Python Tkinter Button Read More »

Tkinter in Python

In this tutorial, we will learn Tkinter in Python. Tkinter is a library which allows us to create Graphical User Interface. We can create desktop-based applications with the help of Tkinter. In this Tkinter tutorial series, we will learn various widgets in Tkinter along with Example programs. Steps to use Tkinter in your Application: Import Tkinter in your program Create the main application window. Add the widgets such as button, label in your main application window Add actions to different widgets in your applications Example Program of Tkinter in Python: #importing tkinter lib from tkinter import * #creating the main window. mainWindow = Tk() #event main loop mainWindow.mainloop() Widgets in Tkinter: Widget Description Button This widget is used to add button in Python application. Canvas This widget is used to make a canvas in Python application. Check Button This widget is used to show options with checkboxes in python application. Entry This widget is used to get a single line entry from the user. Frame This widget is used for grouping of other Tkinter Widgets. Label This widget is used to show caption with other widgets. LabelFrame Tkinter LabelFrame acts as a container and used to create a border around child widgets. ListBox This widget helps up to show list of items in application. MenuButton This widget used to show the drop-down menu. Menu This widget helps us to add a menu in the application. Message Python Tkinter Message works same as Lable expect it can automatically wrap up text. MessageBox Tkinter MessageBox widget is used to show the message box in the application. PanedWindow PanedWindow is a widget which is used as a container. A PanedWindow can contain one or more child Panes. RadioButton This widget is used to show multiple options to the user. Scale Tkinter Scale widget is used to the graphical slider. ScrollBar ScrollBar widget is used to make the content of other widgets scroll-able. Text The Tkinter Text widget is used to show multi-line text in the application TopLevel TopLevel is used to show top-level screen and which directly managed by the window manager. Spinbox Spinbox is an entry widget which is used to ask the user to choose one from fixed values. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Tkinter in Python Read More »

Scroll to Top
×