Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views10 pages

GUI Question

The document contains a series of Python GUI programs using the Tkinter package, demonstrating various functionalities such as creating windows, adding labels and buttons, handling events, creating menus, and implementing a calculator interface. Each program is presented with code snippets that illustrate how to achieve specific tasks within a Tkinter application. The examples cover a range of GUI components including message boxes, checkbuttons, radio buttons, progress bars, and layout management.

Uploaded by

ravisingh302004
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)
6 views10 pages

GUI Question

The document contains a series of Python GUI programs using the Tkinter package, demonstrating various functionalities such as creating windows, adding labels and buttons, handling events, creating menus, and implementing a calculator interface. Each program is presented with code snippets that illustrate how to achieve specific tasks within a Tkinter application. The examples cover a range of GUI components including message boxes, checkbuttons, radio buttons, progress bars, and layout management.

Uploaded by

ravisingh302004
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/ 10

1.

Write a Python GUI program to import Tkinter package and create a


window and set its title.
import tkinter as tk
# Create instance
parent = tk.Tk()
# Add a title
parent.title("-Welcome to Python tkinter Basic exercises-")
# Start GUI
parent.mainloop()

2. Write a Python GUI program to import Tkinter package and create a


window. Set its title and add a label to the window.
import tkinter as tk
parent = tk.Tk()
parent.title("-Welcome to Python tkinter Basic exercises-")
my_label = tk.Label(parent, text="Label widget")
my_label.grid(column=0, row=0)
parent.mainloop()

3. Write a Python GUI program to create a label and change the label font
style (font name, bold, size) using tkinter module.
import tkinter as tk
parent = tk.Tk()
parent.title("-Welcome to Python tkinter Basic exercises-")
my_label = tk.Label(parent, text="Hello", font=("Arial Bold", 70))
my_label.grid(column=0, row=0)
parent.mainloop()

4. Write a Python GUI program that adds labels and buttons to the Tkinter
window.
import tkinter as tk
# Create the main window
parent = tk.Tk()
parent.title("Button Click Event Handling")

# Create a label widget


label = tk.Label(parent, text="Click the button and check the label
text:")
label.pack()
# Function to handle button click event
def on_button_click():
label.config(text="Button Clicked!")

# Create a button widget


button = tk.Button(parent, text="Click Me",
command=on_button_click)
button.pack()

# Start the Tkinter event loop


parent.mainloop()

5. Write a Python program that implements event handling for button


clicks using Tkinter.
import tkinter as tk

# Create the main window


root = tk.Tk()
root.title("Button Click Event Handling")

# Create a label widget


label = tk.Label(root, text="Click the button and check the message
text:")
label.pack()

# Function to handle button click event


def on_button_click():
label.config(text="Button Clicked!")

# Create a button widget


button = tk.Button(root, text="Click Me",
command=on_button_click)
button.pack()

# Start the Tkinter event loop


root.mainloop()

6. Write a Python program that creates a basic menu bar with menu items
using Tkinter.
import tkinter as tk
from tkinter import Menu
# Create the main window
parent = tk.Tk()
parent.title("Spyder (Python 3.6)")

# Create a menu bar


menu_bar = Menu(parent)
parent.config(menu=menu_bar)

# Create a File menu


file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)

# Add menu items to the File menu


file_menu.add_command(label="New")
file_menu.add_command(label="Open")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=parent.quit)

# Create an Edit menu


edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)

# Add menu items to the Edit menu


edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Paste")

# Create a Help menu


help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)

# Add menu items to the Help menu


help_menu.add_command(label="About Spyder")

# Start the Tkinter event loop


parent.mainloop()

Write a Python program that displays messages in a message box using


Tkinter.
import tkinter as tk
from tkinter import messagebox
# Create the main window
parent = tk.Tk()
parent.title("Messagebox Example")

# Function to display a message in a messagebox


def show_message():
messagebox.showinfo("Message", "Hello!")

# Create a button to trigger the messagebox


button = tk.Button(parent, text="Show Message",
command=show_message)
button.pack()

# Start the Tkinter event loop


parent.mainloop()

Write a Python GUI program that creates a window with a specific background
color using Tkinter.

import tkinter as tk
# Create the main window
parent = tk.Tk()
parent.title("Window with Background Color")
# Set the background color of the window
parent.configure(bg="lightpink") # Replace " lightpink" with your
desired color
# Start the Tkinter event loop
parent.mainloop()

7. Write a Python program that customizes the appearance of labels and


buttons (e.g., fonts, colors) using Tkinter.

import tkinter as tk

# Create the main window


parent = tk.Tk()
parent.title("Customizing Labels and Buttons")

# Customize label appearance


label = tk.Label(parent, text="Custom Label", font=("Arial", 18),
fg="white", bg="red")
label.pack(pady=10)

# Customize button appearance


button = tk.Button(parent, text="Custom Button",
font=("Helvetica", 14), fg="white", bg="blue")
button.pack(pady=10)

# Start the Tkinter event loop


parent.mainloop()

8. Write a Python program to design a simple calculator application using


Tkinter with buttons for numbers and arithmetic operations.

import tkinter as tk

# Function to update the display


def update_display(value):
current_text = display_var.get()
if current_text == "0":
display_var.set(value)
else:
display_var.set(current_text + value)

# Function to clear the display


def clear_display():
display_var.set("0")

# Function to evaluate the expression and display the result


def calculate_result():
try:
result = eval(display_var.get())
display_var.set(result)
except Exception as e:
display_var.set("Error")

# Create the main window


parent = tk.Tk()
parent.title("Calculator")

# Create a variable to store the current display value


display_var = tk.StringVar()
display_var.set("0")

# Create the display label


display_label = tk.Label(parent, textvariable=display_var,
font=("Arial", 24), anchor="e", bg="lightgray", padx=10,
pady=10)
display_label.grid(row=0, column=0, columnspan=4)

# Define the button layout


button_layout = [
("7", 1, 0), ("8", 1, 1), ("9", 1, 2), ("/", 1, 3),
("4", 2, 0), ("5", 2, 1), ("6", 2, 2), ("*", 2, 3),
("1", 3, 0), ("2", 3, 1), ("3", 3, 2), ("-", 3, 3),
("0", 4, 0), (".", 4, 1), ("=", 4, 2), ("+", 4, 3),
]

# Create and place the buttons


for (text, row, col) in button_layout:
button = tk.Button(parent, text=text, padx=20, pady=20,
font=("Arial", 18),
command=lambda t=text: update_display(t) if t !=
"=" else calculate_result())
button.grid(row=row, column=col)

# Create a Clear button


clear_button = tk.Button(parent, text="C", padx=20, pady=20,
font=("Arial", 18), command=clear_display)
clear_button.grid(row=5, column=0, columnspan=3)

# Start the Tkinter event loop


parent.mainloop()

9. Write a Python GUI program to add a button in


your application using tkinter module.
import tkinter as tk
parent = tk.Tk()
parent.title('Title - button')
my_button = tk.Button(parent, text='Quit', height=1, width=35,
command=parent.destroy)
my_button.pack()
parent.mainloop()

10. Write a Python GUI program to create two buttons exit and hello
using tkinter module.

import tkinter as tk

def write_text():
print("Tkinter is easy to create GUI!")

parent = tk.Tk()
frame = tk.Frame(parent)
frame.pack()

text_disp= tk.Button(frame, text="Hello", command=write_text )

text_disp.pack(side=tk.LEFT)

exit_button = tk.Button(frame, text="Exit", fg="green",


command= quit )
exit_button.pack(side=tk.RIGHT)

parent.mainloop()

11. Write a Python GUI program to create a Checkbutton widget


using tkinter module.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
my_boolean_var = tk.BooleanVar()

my_checkbutton = ttk.Checkbutton( text="Check when the option


True",
variable=my_boolean_var)
my_checkbutton.pack()
root.mainloop()

12. Write a Python GUI program to create three radio buttons


widgets using tkinter module.
import tkinter as tk
parent = tk.Tk()
parent.title("Radiobutton ")
parent.geometry('350x200')
radio1 = tk.Radiobutton(parent, text='First', value=1)
radio2 = tk.Radiobutton(parent, text='Second', value=2)
radio3 = tk.Radiobutton(parent, text='Third', value=3)
radio1.grid(column=0, row=0)
radio2.grid(column=1, row=0)

13. Write a Python GUI program to create a Progress


bar widgets using tkinter module.

import tkinter as tk
from tkinter.ttk import Progressbar
from tkinter import ttk
parent = tk.Tk()
parent.title("Progressbar")
parent.geometry('350x200')
style = ttk.Style()
style.theme_use('default')
style.configure("black.Horizontal.TProgressbar",
background='green')
bar = Progressbar(parent, length=220,
style='black.Horizontal.TProgressbar')
bar['value'] = 80
bar.grid(column=0, row=0)
parent.mainloop()

14. Write a Python program to create a Tkinter window with three


labels arranged vertically using the Pack geometry manager.
import tkinter as tk
# Create the main Tkinter window
parent = tk.Tk()
parent.title("Vertical Labels with Pack")
# Create three label widgets
label1 = tk.Label(parent, text="Python Exercises", padx=10,
pady=5)
label2 = tk.Label(parent, text="Java Exercises", padx=10, pady=5)
label3 = tk.Label(parent, text="C++ Exercises", padx=10, pady=5)
# Use the Pack geometry manager to arrange labels vertically
label1.pack(side="top")
label2.pack(side="top")
label3.pack(side="top")
# Run the Tkinter main loop
parent.mainloop()

15. Write a Python program that develops a calculator interface with


buttons for digits and operators, arranging them in a grid.
import tkinter as tk

# Function to update the calculator display


def update_display(value):
current_text = display.get()
new_text = current_text + value
display.set(new_text)

# Function to calculate and display the result


def calculate_result():
try:
result = eval(display.get())
display.set(str(result))
except Exception as e:
display.set("Error")

# Function to clear the calculator display


def clear_display():
display.set("")

# Create the main Tkinter window


parent = tk.Tk()
parent.title("Calculator")

# Create a StringVar to store the display value


display = tk.StringVar()
display.set("")

# Create the calculator display Entry widget


display_entry = tk.Entry(parent, textvariable=display,
font=("Arial", 18), justify="right")
display_entry.grid(row=0, column=0, columnspan=4, padx=10,
pady=10, ipadx=10, ipady=10)
# Define the button labels for digits, operators, and special
functions
button_labels = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]

# Create and arrange the buttons in a grid


row_val = 1
col_val = 0

for label in button_labels:


if label == '=':
tk.Button(parent, text=label, padx=20, pady=20,
font=("Arial", 16), command=calculate_result).grid(row=row_val,
column=col_val)
elif label == 'C':
tk.Button(parent, text=label, padx=20, pady=20,
font=("Arial", 16), command=clear_display).grid(row=row_val,
column=col_val)
else:
tk.Button(parent, text=label, padx=20, pady=20,
font=("Arial", 16), command=lambda l=label:
update_display(l)).grid(row=row_val, column=col_val)

col_val += 1
if col_val > 3:
col_val = 0
row_val += 1

# Run the Tkinter main loop


parent.mainloop()

You might also like