Jul 14, 2024
import tkinter as tk
from tkinter import *
to prevent ambiguity.tk.Tk()
root = tk.Tk()
root.mainloop()
root.mainloop()
root.title('title')
root.geometry('widthxheight')
root.title('My First GUI')
root.geometry('500x500')
label = tk.Label(root, text="Hello World", font=('Arial', 18))
label.pack(padx=20, pady=20)
text_box = tk.Text(root, font=('Arial', 16), height=3)
text_box.pack(padx=20)
entry = tk.Entry(root)
entry.pack(padx=10)
button = tk.Button(root, text="Click Me", font=('Arial', 18))
button.pack(padx=10, pady=10)
button_frame = tk.Frame(root)
button_frame.columnconfigure(0, weight=1)
button1 = tk.Button(button_frame, text="1", font=('Arial', 18))
button1.grid(row=0, column=0, sticky='we')
button_frame.pack(fill='x')
another_button = tk.Button(root, text="Test")
another_button.place(x=200, y=200, height=100, width=100)
class MyGUI:
def __init__(self):
self.root = tk.Tk()
self.root.mainloop()
# Add more initiation here...
def show_message(self):
message = self.text_box.get("1.0", tk.END)
if self.check_state.get() == 0:
print(message)
else:
messagebox.showinfo("Message", message)
button.config(command=self.show_message)
Menu Bar
menu_bar = tk.Menu(root)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Close", command=root.quit)
menu_bar.add_cascade(label="File", menu=file_menu)
root.config(menu=menu_bar)
Closing Event
def on_closing(self):
if messagebox.askyesno("Quit", "Do you want to quit?"):
self.root.destroy()
self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
Keyboard Shortcuts
def shortcut(self, event):
if event.state == 12 and event.keysym == 'Return':
self.show_message()
self.text_box.bind('<KeyPress>', self.shortcut)