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#Creating Tkinter Window In Python:
2
3from tkinter import *
4
5new_window = Tk() #Create a window ; spaces should be denoted with underscores ; every window should have a different name
6new_window.title("My Python Project") #Name of screen ; name should be the one which you already declared (new_window)
7new_window.geometry("200x150") #Resizes the default window size
8new_window.configure(bg = "red") #Gives color to the background
9
10new_window.mainloop() #Shows the window on the screen
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
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())
1#!/usr/bin/python
2
3import Tkinter
4top = Tkinter.Tk()
5# Code to add widgets will go here...
6top.mainloop()
1button = tk.Button(
2 text="Click me!",
3 width=25,
4 height=5,
5 bg="blue",
6 fg="yellow",
7)
8