1from tkinter import Tk, Label, Button
2
3class MyFirstGUI:
4 def __init__(self, master):
5 self.master = master
6 master.title("A simple GUI")
7
8 self.label = Label(master, text="This is our first GUI!")
9 self.label.pack()
10
11 self.greet_button = Button(master, text="Greet", command=self.greet)
12 self.greet_button.pack()
13
14 self.close_button = Button(master, text="Close", command=master.quit)
15 self.close_button.pack()
16
17 def greet(self):
18 print("Greetings!")
19
20root = Tk()
21my_gui = MyFirstGUI(root)
22root.mainloop()
23
1# check this code first.
2from tkinter import *
3
4app = Tk()
5# The title of the project
6app.title("The title of the project")
7# The size of the window
8app.geometry("400x400")
9
10# Defining a funtion
11def c():
12 # Label
13 m = Label(app, text="Text")
14 m.pack()
15
16
17# Button
18l = Button(app, text="The text of the Butoon", command=c)
19# Packing the Button
20l.pack()
21app.mainloop()
22# Quick Note :
23# When you put a command you should not use parentheses
24# l = Button(app, text="The text of the Butoon", command=c)
25# l = Button(app, text="The text of the Butoon", command=c())
1from tkinter import *
2from tkinter import ttk
3
4def calculate(*args):
5 try:
6 value = float(feet.get())
7 meters.set(int(0.3048 * value * 10000.0 + 0.5)/10000.0)
8 except ValueError:
9 pass
10
11root = Tk()
12root.title("Feet to Meters")
13
14mainframe = ttk.Frame(root, padding="3 3 12 12")
15mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
16root.columnconfigure(0, weight=1)
17root.rowconfigure(0, weight=1)
18
19feet = StringVar()
20feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
21feet_entry.grid(column=2, row=1, sticky=(W, E))
22
23meters = StringVar()
24ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
25
26ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)
27
28ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
29ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
30ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)
31
32for child in mainframe.winfo_children():
33 child.grid_configure(padx=5, pady=5)
34
35feet_entry.focus()
36root.bind("<Return>", calculate)
37
38root.mainloop()
39
1import tkinter as tk
2
3window = tk.Tk()
4
5frame_a = tk.Frame()
6frame_b = tk.Frame()
7
8label_a = tk.Label(master=frame_a, text="I'm in Frame A")
9label_a.pack()
10
11label_b = tk.Label(master=frame_b, text="I'm in Frame B")
12label_b.pack()
13
14frame_a.pack()
15frame_b.pack()
16
17window.mainloop()
18
1#Import
2import tkinter as tk
3from tkinter import ttk
4#Main Window
5window=tk.Tk()
6#Label
7label=ttk.Label(window,text="My First App")
8#Display label
9label.pack()
10#Making sure that if you press the close button of window, it closes.
11window.mainloop()