tkinter assignment

 Fundamental structure of a tkinter program



Do the following programs to learn the concept


Hello world Basic tkinter program


from tkinter import *
# writing code needs to
# create the main window of
# the application creating
# main window object named root
root = Tk()

# giving title to the main window 400x400
root.geometry('400x400')
root.title("First_Program")

# Label is what output will be
# show on the window
label = Label(root, text ="Hello World !")
label.pack()
#button shows a command
button = Button(root, text ='Close',command=root.destroy) 
button.pack(side='bottom')      
# calling mainloop method which is used
# when your application is ready to run
# and it tells the code to keep displaying
root.mainloop()


Drawing some simple shapes in canvas

from tkinter import *
root = Tk()
C = Canvas(root, bg="yellow",height=250, width=300)
line = C.create_line(108,120,320, 40,fill="green")
arc = C.create_arc(180, 150, 80,210, start=0,extent=220,fill="red")
oval = C.create_oval(80, 30, 140,150,fill="blue")

C.pack()
mainloop()

Creating a simple Menu

# importing only those functions
# which are needed
from tkinter import *
# creating tkinter window
root = Tk()
root.title('Menu Demonstration')

# Creating Menubar
menubar = Menu(root)
# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)

# Adding Help Menu
help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)

# display Menu
root.config(menu = menubar)

mainloop()

Write a Python GUI Program to Find the area of the circle
import tkinter as tk

def area():
        r= float(entry.get())
        a = 3.14*r*r
        result.config(text=f"Area={a}")

# Create the main window
root = tk.Tk()
root.title("Area of Circle")

# Create input fields
label = tk.Label(root, text="Enter radius:")
label.pack()

entry = tk.Entry(root)
entry.pack()

# Create a button to trigger the computation
add_button = tk.Button(root, text="Find Area", command=area)
add_button.pack()

# Label to display the result
result = tk.Label(root, text="Area: ")
result.pack()

# Start the main event loop
root.mainloop()

Write Python GUI program to take the birth date and output the age when a button is pressed.
( university question)
from tkinter import *
from datetime import date
def FindAge():
    d=e1.get()
    d=d.split('/')
    birthdate=date(int(d[2]),int(d[1]),int(d[0]))
    today = date.today()
    age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day))
    myText.set(age)
master=Tk()
myText=StringVar()
Label(master, text="Date").grid(row=0, sticky=W)
Label(master, text="Age").grid(row=1, sticky=W)

result=Label(master, text="", textvariable=myText).grid(row=1,column=1, sticky=W)

e1 = Entry(master)
e1.grid(row=0, column=1)

b = Button(master, text="Calculate", command=FindAge)
b.grid(row=0, column=2,columnspan=2, rowspan=2,sticky=W+E+N+S, padx=5, pady=5)
mainloop()


Now you can do the following programs. All program output must be there in the pdf submission. Give file name as yourname-rollnumber.pdf

1.Write a GUI-based program that allows the user to convert temperature values between degrees Fahrenheit and degrees Celsius. The interface should have labeled entry fields for these two values. These components should be arranged in a grid where the labels occupy the first row and the corresponding fields occupy the second row. At start-up, the Fahrenheit field should contain 32.0, and the Celsius field should contain 0.0. The third row in the window contains two command buttons, labeled >>>> and <<<<. When the user presses the first button, the program should use the data in the Fahrenheit field to compute the Celsius value, which should then be output to the Celsius field. The second button should perform the inverse function.


2.Write a GUI-based program that allows the user to convert amount in Indian Rupees to amount in Euro. The interface should have labeled entry fields for these two values. These components should be arranged in a grid where the labels occupy the first row and the corresponding fields occupy the second row. At start-up, the Rupees field should contain 0.0, and the Euro field should contain 0.0. The third row in the window contains two command buttons, labeled R->E and E->R. When the user presses the first button, the program should use the data in the Rupee field to compute the amount in Euro, which should then be output to the Euro field. The second button should perform the inverse function.




Comments

Popular posts from this blog

Programming in Python CST 362 KTU CS Sixth Semester Elective Notes

Graphical User Interfaces ( GUI)

Turtle Graphics