0% found this document useful (0 votes)
11 views14 pages

Python GUI Programming Examples

The document contains a series of Python code examples using the Tkinter library to create GUI applications. It includes codes for opening a window, displaying text, accepting user input, performing arithmetic operations, and creating menus with radio buttons. Each example is accompanied by the code and intended output, demonstrating various GUI functionalities.

Uploaded by

Sajid Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views14 pages

Python GUI Programming Examples

The document contains a series of Python code examples using the Tkinter library to create GUI applications. It includes codes for opening a window, displaying text, accepting user input, performing arithmetic operations, and creating menus with radio buttons. Each example is accompanied by the code and intended output, demonstrating various GUI functionalities.

Uploaded by

Sajid Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

NAME- SAJID AHMED Roll no-001 DATE-8/2/2023

PRACTISE QUESTIONS (NOT OF PRACTICAL)


Q1] Write a python code to open a simple GUI window CHANGE THE title of the
window
CODE:
import tkinter as t
w=[Link]()
[Link]("GUI")
[Link]()
OUTPUT:

Q2] Write a python code to display text ‘Hello’


CODE:
import tkinter as t
w=[Link]()
[Link]("GUI")
q=[Link](text='Hello')
[Link]()
[Link]()
OUTPUT:

Q3] Write a python code to change height ,width ,background color of window
CODE:

1
import tkinter as t
w=[Link]()
[Link]("GUI")
[Link]("300x200")
q=[Link](
text='Hello',
fg="white",
bg="blue",
width="20",
height="10"
)
[Link]()
[Link]()
OUTPUT:

Q4] Write a python code to display entry


CODE:
import tkinter as t
w=[Link]()
[Link]("GUI")
[Link]("300x200")
q=[Link](
text='Hello',

2
fg="white",
bg="blue",
width="20",
height="10"
)
e=[Link](width=20)
[Link]()
[Link]()
[Link]()
OUTPUT:

Q5] Write a python code to display button


CODE:
import tkinter as t
w=[Link]()
[Link]("GUI")
[Link]("300x200")
q=[Link](text='Hello',fg="white",bg="black",width="20",height="5")
[Link]()
e=[Link](width=20)
[Link]()
b=[Link](text="click me")
[Link]()

3
[Link]()
OUTPUT:

Q6] Write a python code to display entered statement in the window text
CODE:
import tkinter as t
w=[Link]()
[Link]("GUI")
[Link]("300x200")
def b1():
s=[Link]()
[Link](text=s)

q=[Link](text='Hello',fg="white",bg="black",width="20",height="5")
[Link]()
e=[Link](width=20)
[Link]()
b=[Link](text="click me",command=b1)
[Link]()
[Link] oop()
OUTPUT:

4
Q7] Write a python program to accept name from user using GUI OR TKINTER and
show a button with text=’show’ on clicking the button the label should show ‘hello’
followed by data taken from user
CODE:
import tkinter as t
m=[Link]()
[Link]("GUI")
e=[Link](width=20)
[Link]()
def b1():
s=[Link]()
a='Hello ' + s
[Link](text=a)
[Link]("300x200")

b=[Link](text='Click here',command=b1)

5
[Link]()

q=[Link](fg="white",bg='black')
[Link]()
[Link]()

OUTPUT:

Q8] Write a python program USING GUI to accept two numbers from user and
perform addition of that numbers and display result
CODE:
import tkinter as t
m=[Link]()
[Link]("ADDITION")
z=[Link](text='First Number')
[Link]()
e=[Link](width=20)
[Link]()
w=[Link](text='Second Number')
[Link]()
f=[Link](width=20)

6
[Link]()

def b1():
s=int([Link]())
p=int([Link]())
a=p+s
[Link](text=a)
[Link]("300x200")

b=[Link](text="Click here",command=b1)
[Link]()

q=[Link](fg="white",bg='Black')
[Link]()
[Link]()

OUTPUT:

Practical 3

7
Aim: Programs based on GUI programming
1. To accept two numbers and show the result of sum difference , multiplication
and division
Code:
import tkinter as t
m=[Link]()
[Link]("calculator")
z=[Link](text='First Number')
[Link]()
e=[Link](width=20)
[Link]()
w=[Link](text='Second Number')
[Link]()
f=[Link](width=20)
[Link]()

def b1():
s=int([Link]())
p=int([Link]())
a=s+p
[Link](text=a)
[Link]("300x200")

def b2():
s=int([Link]())
p=int([Link]())
b=s-p
[Link](text=b)
[Link]("300x200")
8
def b3():
s=int([Link]())
p=int([Link]())
c=s*p
[Link](text=c)
[Link]("300x200")

def b4():
s=int([Link]())
p=int([Link]())
d=s/p
[Link](text=d)
[Link]("300x200")

b=[Link](text="Addition",command=b1)
[Link]()

b=[Link](text="Subtraction",command=b2)
[Link]()

b=[Link](text="Multiplication",command=b3)
[Link]()

b=[Link](text="Division",command=b4)
[Link]()

9
q=[Link](text="The result is",fg="white",bg='Black')
[Link]()

[Link]()
Output:

2. Write the code to show menu of pizza ,burger ,pasta, biryani in radio button and
ask the choice and display the choice
Code:
import tkinter as t
w=[Link]()
[Link]('300x300')
q1=[Link](w,text="MENU")
[Link](column=2,row=1)
def p():
z=[Link]()
[Link](text=z)
x=[Link]()
r1=[Link](w,text="Pizza",value="Pizza",variable=x,command=p)
[Link](column=1,row=2)
r2=[Link](w,text="Burger",value="Burger",variable=x,command=p)

10
[Link](column=1,row=3)
r3=[Link](w,text="Pasta",value="Pasta",variable=x,command=p)
[Link](column=1,row=4)
r4=[Link](w,text="Biryani",value="Biryani",variable=x,command=p)
[Link](column=1,row=5)
q2=[Link](w,text="What is your Choice?")
[Link](column=1,row=6)

[Link]()
Output:

3. To calculate sales tax. Tax rate can vary 1 to 20%


Code:
import tkinter as t
11
m=[Link]()
[Link]("Sales Tax")
[Link]("300x200")

p1=[Link](text="Price")
[Link](column=1,row=1)
e=[Link](width=25)
[Link](column=2,row=1)

p2=[Link](text="Tax Rate %")


[Link](column=1,row=2)
sp=[Link](m,from_=0,to=10)
[Link](column=2,row=2)

v2=[Link]()

def tax():
v=int([Link]())
v1=int([Link]())
f=[Link]()
cal=(v+v1)/100
cal1=cal+v1
cal2=str(cal1)
[Link](text=cal2+f)

[Link](column=3,row=3)
r3=[Link](text="Euros",value="Euros",variable=v2)
p2=[Link](text="Amount is:")

12
[Link](column=1,row=3)
r1=[Link](text="Rupees",value="Rupees",variable=v2)
[Link](column=2,row=3)
r2=[Link](text="Dollars",value="Dollars",variable=v2)

[Link](column=4,row=3)

b1=[Link](text='Calculate Tax',command=tax)
[Link](column=2,row=4)

lab1=[Link](text='Value')
[Link](column=2,row=5)

[Link]()
Output:

4. To draw line ,arc ,polygon in canvas


Code:
import tkinter as t
w=[Link]()
c=[Link](w,bg="black",height=350,width=350)
l=c.create_line(100,100,20,20,fill="red")

13
a=c.create_arc(120,120,200,200,start=0,extent=120,fill='cyan')
p=c.create_polygon(10,200,100,150,200,200,200,300,100,350,10,300,fill='red')
[Link]()
Output:

14

Common questions

Powered by AI

To create a GUI window with Tkinter where the title is changed, you import the Tkinter module, create a Tkinter object using `t.Tk()`, and then use the `title()` method to set the desired title. For example: `import tkinter as t; w=t.Tk(); w.title("GUI"); w.mainloop()` will create a window with the title 'GUI' .

Use Tkinter's geometry managers—`pack()`, `grid()`, and `place()`—for layout customization. `pack()` organizes widgets in blocks, defaulting to top-to-bottom, allowing alignment and padding adjustment. `grid()` places widgets in a 2D table using row and column specs, enabling precise control over placement. `place()` provides exact positioning via x and y coordinates, offering flexibility for complex layouts. These mechanisms enhance widget placement, accommodate dynamic content and respond to window resizing [conceptual application from Source 1].

To display user input on a label in Tkinter when a button is clicked, create an entry widget for user input, a label to display the output, and a button to trigger the command. Define a function that retrieves the input text using `get()` method of the entry and updates the label with the retrieved text using `configure()`. For example: `import tkinter as t; w=t.Tk(); e=t.Entry(width=20); e.pack(); q=t.Label(); q.pack(); def show_text(): s=e.get(); q.config(text=s); b=t.Button(text='Click Me',command=show_text); b.pack(); w.mainloop()` .

In Tkinter, create two entry widgets for number input and buttons for each arithmetic operation. Each button will have an associated command function to perform the operation. Retrieve values from entries, convert them to integers, and perform the desired operation, updating a label with the result. For example, a button for addition will have a function that sums numbers from the entries and updates the label: `def add(): result=int(entry1.get())+int(entry2.get()); label.config(text=str(result));`. Repeat similar logic for subtraction, multiplication, and division with respective functions .

The `Canvas` widget in Tkinter provides powerful features for drawing shapes. Use methods like `create_line()`, `create_arc()`, and `create_polygon()` for drawing. Invoke these methods on the `Canvas` object, specifying coordinates and optional parameters such as fill colors. For example, `create_line(10, 10, 200, 200, fill='red')` draws a red line, `create_arc(100, 100, 150, 150, start=0, extent=120, fill='blue')` draws a blue arc, and `create_polygon(x1, y1, x2, y2, ... , fill='green')` creates a green polygon .

The `geometry()` method in Tkinter allows you to set both the size and the position of a window defined by strings such as 'widthxheight' or 'widthxheight+x_offset+y_offset'. To implement this in a Tkinter application, use the method after creating the main window object: `w.geometry("300x200")` sets the window size to 300px width and 200px height .

In Tkinter, the `Radiobutton` widget allows users to select one option from a set of choices. Assign each `Radiobutton` a `value` and a shared `variable` to store the selected option. Define a callback function to execute when a `Radiobutton` is selected, usually to update a label or process input. Example: define `variable` with `StringVar()`, create each radio button with corresponding `value` and `command`, and a label to display selected value using `configure()`. Example configuration: `q2.configure(text=z)` after `z=x.get()` in the callback function .

To implement a sales tax calculator using Tkinter, create entry widgets for price and tax rate input, and a Spinbox for tax percentage selection. Define a function that calculates the tax based on inputs: multiply the tax rate by the price and divide by 100, add the result to the initial price, and display using a label. Example code utilizes `get()` method to retrieve and convert inputs for calculations: `def tax(): rate=int(spinbox.get()); price=int(entry.get()); total=(price*(rate/100))+price; label.config(text=str(total));` .

In Tkinter, create a GUI with an `Entry` widget for data input, a `Button` widget to trigger label update, and a `Label` widget to display the input. Attach a command to the button that retrieves the entry's text with the `get()` method and updates the label using `configure()`. This creates a dynamic data entry form where interaction changes the display. Code sample: `def update_label(): label.config(text=entry.get()); button = Button(text='Submit', command=update_label); button.pack()` [Derived concept].

Implement exception handling around user input conversion to manage errors, like `ValueError` from non-numeric input. Use a `try-except` block within functions that process input, placing `int()` or `float()` conversion inside the `try` block. If an exception occurs, inform the user via a dialog box or label update. Example: `try: num=int(entry.get()); except ValueError: label.config(text='Invalid input')` informs users upon error detection and maintains robustness [implied best practices].

You might also like