[go: up one dir, main page]

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

Ak Python p3

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)
12 views14 pages

Ak Python p3

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
You are on page 1/ 14

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=t.Tk()
w.title("GUI")
w.mainloop()
OUTPUT:

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


CODE:
import tkinter as t
w=t.Tk()
w.title("GUI")
q=t.Label(text='Hello')
q.pack()
w.mainloop()
OUTPUT:

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

1
import tkinter as t
w=t.Tk()
w.title("GUI")
w.geometry("300x200")
q=t.Label(
text='Hello',
fg="white",
bg="blue",
width="20",
height="10"
)
q.pack()
w.mainloop()
OUTPUT:

Q4] Write a python code to display entry


CODE:
import tkinter as t
w=t.Tk()
w.title("GUI")
w.geometry("300x200")
q=t.Label(
text='Hello',

2
fg="white",
bg="blue",
width="20",
height="10"
)
e=t.Entry(width=20)
e.pack()
q.pack()
w.mainloop()
OUTPUT:

Q5] Write a python code to display button


CODE:
import tkinter as t
w=t.Tk()
w.title("GUI")
w.geometry("300x200")
q=t.Label(text='Hello',fg="white",bg="black",width="20",height="5")
q.pack()
e=t.Entry(width=20)
e.pack()
b=t.Button(text="click me")
b.pack()

3
w.mainloop()
OUTPUT:

Q6] Write a python code to display entered statement in the window text
CODE:
import tkinter as t
w=t.Tk()
w.title("GUI")
w.geometry("300x200")
def b1():
s=e.get()
q.configure(text=s)

q=t.Label(text='Hello',fg="white",bg="black",width="20",height="5")
q.pack()
e=t.Entry(width=20)
e.pack()
b=t.Button(text="click me",command=b1)
b.pack()
w.mainl 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=t.Tk()
m.title("GUI")
e=t.Entry(width=20)
e.pack()
def b1():
s=e.get()
a='Hello ' + s
q.configure(text=a)
m.geometry("300x200")

b=t.Button(text='Click here',command=b1)

5
b.pack()

q=t.Label(fg="white",bg='black')
q.pack()
m.mainloop()

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=t.Tk()
m.title("ADDITION")
z=t.Label(text='First Number')
z.pack()
e=t.Entry(width=20)
e.pack()
w=t.Label(text='Second Number')
w.pack()
f=t.Entry(width=20)

6
f.pack()

def b1():
s=int(e.get())
p=int(f.get())
a=p+s
q.configure(text=a)
m.geometry("300x200")

b=t.Button(text="Click here",command=b1)
b.pack()

q=t.Label(fg="white",bg='Black')
q.pack()
m.mainloop()

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=t.Tk()
m.title("calculator")
z=t.Label(text='First Number')
z.pack()
e=t.Entry(width=20)
e.pack()
w=t.Label(text='Second Number')
w.pack()
f=t.Entry(width=20)
f.pack()

def b1():
s=int(e.get())
p=int(f.get())
a=s+p
q.configure(text=a)
m.geometry("300x200")

def b2():
s=int(e.get())
p=int(f.get())
b=s-p
q.configure(text=b)
m.geometry("300x200")
8
def b3():
s=int(e.get())
p=int(f.get())
c=s*p
q.configure(text=c)
m.geometry("300x200")

def b4():
s=int(e.get())
p=int(f.get())
d=s/p
q.configure(text=d)
m.geometry("300x200")

b=t.Button(text="Addition",command=b1)
b.pack()

b=t.Button(text="Subtraction",command=b2)
b.pack()

b=t.Button(text="Multiplication",command=b3)
b.pack()

b=t.Button(text="Division",command=b4)
b.pack()

9
q=t.Label(text="The result is",fg="white",bg='Black')
q.pack()

m.mainloop()
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=t.Tk()
w.geometry('300x300')
q1=t.Label(w,text="MENU")
q1.grid(column=2,row=1)
def p():
z=x.get()
q2.configure(text=z)
x=t.StringVar()
r1=t.Radiobutton(w,text="Pizza",value="Pizza",variable=x,command=p)
r1.grid(column=1,row=2)
r2=t.Radiobutton(w,text="Burger",value="Burger",variable=x,command=p)

10
r2.grid(column=1,row=3)
r3=t.Radiobutton(w,text="Pasta",value="Pasta",variable=x,command=p)
r3.grid(column=1,row=4)
r4=t.Radiobutton(w,text="Biryani",value="Biryani",variable=x,command=p)
r4.grid(column=1,row=5)
q2=t.Label(w,text="What is your Choice?")
q2.grid(column=1,row=6)

w.mainloop()
Output:

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


Code:
import tkinter as t
11
m=t.Tk()
m.title("Sales Tax")
m.geometry("300x200")

p1=t.Label(text="Price")
p1.grid(column=1,row=1)
e=t.Entry(width=25)
e.grid(column=2,row=1)

p2=t.Label(text="Tax Rate %")


p2.grid(column=1,row=2)
sp=t.Spinbox(m,from_=0,to=10)
sp.grid(column=2,row=2)

v2=t.StringVar()

def tax():
v=int(sp.get())
v1=int(e.get())
f=v2.get()
cal=(v+v1)/100
cal1=cal+v1
cal2=str(cal1)
lab1.configure(text=cal2+f)

r2.grid(column=3,row=3)
r3=t.Radiobutton(text="Euros",value="Euros",variable=v2)
p2=t.Label(text="Amount is:")

12
p2.grid(column=1,row=3)
r1=t.Radiobutton(text="Rupees",value="Rupees",variable=v2)
r1.grid(column=2,row=3)
r2=t.Radiobutton(text="Dollars",value="Dollars",variable=v2)

r3.grid(column=4,row=3)

b1=t.Button(text='Calculate Tax',command=tax)
b1.grid(column=2,row=4)

lab1=t.Label(text='Value')
lab1.grid(column=2,row=5)

m.mainloop()
Output:

4. To draw line ,arc ,polygon in canvas


Code:
import tkinter as t
w=t.Tk()
c=t.Canvas(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')
c.pack()
Output:

14

You might also like