Table of contents
1.
Introduction
2.
Tkinter
2.1.
Classes
3.
Simple GUI Calculator using Tkinter
4.
FAQs
4.1.
What is a pyglet?
4.2.
Why is pyglet used?
4.3.
What is pyglet.gui?
4.4.
What is Tkinter ?
4.5.
What is the difference between Django and Tkinter?
5.
Key Takeaways
Last Updated: Mar 27, 2024
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pyglet is a library provided by python to develop games, cross-platform windowing, and multimedia. Pyglet supports user interface event handling, windowing, game controllers, graphics, loading images, videos, etc. The pyglet library can be imported and works on various operating systems like Windows, OS X, and Linux. GUI is a computer program that enables a person to communicate with a computer through symbols, visual metaphors, and pointing devices.

Tkinter

Tkinter is a standard GUI library. Python combines with Tkinter, which provides a fast and easy way to create GUI applications.

from tkinter import *
master = Tk()
Label(master, text='First Name').grid(row=0) 
Label(master, text='Last Name').grid(row=1) 
a1 = Entry(master) 
a2 = Entry(master)
a1.grid(row=0, column=1) 
a2.grid(row=1, column=1) 
mainloop()
You can also try this code with Online Python Compiler
Run Code


Output

Source

screenName

When a string is given,  the screenName sets the DISPLAY variable.

baseName

Give a name for the profile. By default, baseName is taken  from the program name (sys.argv[0]).

Classes

In this, we create a class name with attributes of x,y, X is the coordinate of the widget, and Y is the coordinate of the widget, Whereas width indicates the width of the widget and height indicates the height of the widgets. Both are the integer type.

classWidgetBase(x, y, width, height)

UseTk

If True, initialize the Tk subsystem. The Tkinter.Tcl() function sets this to False.

Sync

If True, execute all X server commands synchronously so that errors get informed immediately. This can be used for debugging.

Use

It specifies the window's id to embed the application instead of creating an independent top-level window. The id must be specified in the same way as the value for the -use option for top-level widgets. 

Syntax for id: winfo_id()

Note that this will only work correctly on some platforms if id refers to a Tk frame or top-level that has its -container option enabled.

Tk reads and interprets profile files, named .className.tcl and .baseName.tcl, into the Tcl interpreter and calls exec() on the contents of .className.py and .baseName.py. The path for the profile files is the HOME environment variable or, if that isn’t defined, then os.curdir.

Simple GUI Calculator using Tkinter

To create a Tkinter :

  • Import the module Tkinter.
  • Create the main window for the calculator
  • Add the number of widgets for the main window
  • Now apply event trigger on widgets


Let's create a simple GUI calculator using Tkinter.

# import everything from the tkinter module

from tkinter import *

expression = ""

def press(num):
	global expression
	expression = expression + str(num)
    equation.set(expression)
 
def equalpress():
	try:
		global expression
		total = str(eval(expression))
     	equation.set(total)
    	expression = "”
	except:
		equation.set(" error ")
  	    expression = ""
  	    
def clear():
	global expression
	expression = ""
    equation.set("")
 
if __name__ == "__main__":
	gui = Tk()
 	gui.configure(background="light green")
	gui.title("Calculator")
	gui.geometry("270x150")
 	equation = StringVar()    
	expression_field = Entry(gui, textvariable=equation)
  	expression_field.grid(columnspan=4, ipadx=70)
  	
	button1 = Button(gui, text=' 1 ', fg='black', bg='red',
                    command=lambda: press(1), height=1, width=7)
                    
  	button1.grid(row=2, column=0)
  	
 	button2 = Button(gui, text=' 2 ', fg='black', bg='red',
                    command=lambda: press(2), height=1, width=7)
                       
	button2.grid(row=2, column=1)
 
    button3 = Button(gui, text=' 3 ', fg='black', bg='red',
                    command=lambda: press(3), height=1, width=7)
                    
	button3.grid(row=2, column=2)
	
 	button4 = Button(gui, text=' 4 ', fg='black', bg='red',
                    command=lambda: press(4), height=1, width=7)
                    
  	button4.grid(row=3, column=0)
  	
   	button5 = Button(gui, text=' 5 ', fg='black', bg='red',
                    command=lambda: press(5), height=1, width=7)
                    
	button5.grid(row=3, column=1)
	
	button6 = Button(gui, text=' 6 ', fg='black', bg='red',
                    command=lambda: press(6), height=1, width=7)
                    
  	button6.grid(row=3, column=2)
  	
   	button7 = Button(gui, text=' 7 ', fg='black', bg='red',
                    command=lambda: press(7), height=1, width=7)
                    
	button7.grid(row=4, column=0)
	
  	button8 = Button(gui, text=' 8 ', fg='black', bg='red',
                    command=lambda: press(8), height=1, width=7)
                    
   	button8.grid(row=4, column=1)
   	
 	button9 = Button(gui, text=' 9 ', fg='black', bg='red',
                    command=lambda: press(9), height=1, width=7)
                    
	button9.grid(row=4, column=2)
	
	button0 = Button(gui, text=' 0 ', fg='black', bg='red',
                    command=lambda: press(0), height=1, width=7)
                    
  	button0.grid(row=5, column=0)
  	
 	plus = Button(gui, text=' + ', fg='black', bg='red',
                command=lambda: press("+"), height=1, width=7)
                
  	plus.grid(row=2, column=3)
  	
 	minus = Button(gui, text=' - ', fg='black', bg='red',
                command=lambda: press("-"), height=1, width=7)
                
 	minus.grid(row=3, column=3)
 	
	multiply = Button(gui, text=' * ', fg='black', bg='red',
                    command=lambda: press("*"), height=1, width=7)
                    
  	multiply.grid(row=4, column=3)
  	
	divide = Button(gui, text=' / ', fg='black', bg='red',
                    command=lambda: press("/"), height=1, width=7)
                    
	divide.grid(row=5, column=3)
	
	equal = Button(gui, text=' = ', fg='black', bg='red',
                command=equalpress, height=1, width=7)
                
	equal.grid(row=5, column=2)
	
	clear = Button(gui, text='Clear', fg='black', bg='red',
                command=clear, height=1, width=7)
                
	clear.grid(row=5, column='1')
	Decimal= Button(gui, text='.', fg='black', bg='red',
	command=lambda: press('.'), height=1, width=7)
	Decimal.grid(row=6, column=0)
	gui.mainloop()
You can also try this code with Online Python Compiler
Run Code


Output:

Source

We created a simple main window with different widgets for the calculator the above. Using conditions, we can add different colors to widgets; using rows and columns, we create the calculator and add the numbers to it. Not only numbers but also we can add clear buttons and different types of symbols like Arithmetic operators. We can add our background colors by using configuration. We add color, text, height, and width for every button.

FAQs

What is a pyglet?

Pyglet is a library provided by python to develop games, cross-platform windowing, and multimedia. Pyglet supports user interface event handling, windowing, game controllers, graphics, loading images, videos, etc. It can be imported and works on various operating systems like Windows, OS X, and Linux. 

Why is pyglet used?

Pyglet is used to build games and other visual applications. It provides classes and submodules that can style the documents, text files, HTML files, etc. It provides font, size, layout, position, etc., for the content.

What is pyglet.gui?

Pyglet.gui is an extension mainly used for graphical user interface and visually rich applications in python. It provides unique APIs to define custom themes, graphics, and user interfaces.

What is Tkinter ?

Tkinter module is mainly used in GUI applications. It is a standard python interface and an in-built module. Tkinter is the fastest and easiest module that helps us to create GUI applications.

What is the difference between Django and Tkinter?

Django explains web frameworks and helps us to build websites and web apps. Coming to Tkinter is a module used in python where we can create the user forms.

Key Takeaways

We have discussed the concept of the pyglet and the module pyglet.gui in python in this article. 

Hey Ninjas! We hope this blog helped you better to understand the pyglet.gui module in python. Please check out Coding Ninjas for more unique courses and guidedpaths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass