In this post I’ll show you how to make a tkinter window.
Example 1: how to create a tkinter window
1 2 3 4 5 6 7 8 | #!/usr/bin/python import tkinter top = tkinter.Tk() # Code to add widgets will go here... top.mainloop() |
Output:
Example 2: Make a window tkinter
1 2 3 4 5 6 7 8 9 10 11 12 | #Creating Tkinter Window In Python: from tkinter import * new_window = Tk() #Create a window ; spaces should be denoted with underscores ; every window should have a different name new_window.title("My Python Project") #Name of screen ; name should be the one which you already declared (new_window) new_window.geometry("200x150") #Resizes the default window size new_window.configure(bg = "red") #Gives color to the background new_window.mainloop() #Shows the window on the screen |
Output:
Example 3:
1 2 3 4 5 6 7 8 9 10 11 | from tkinter import * mywindow = Tk() #Change the name for every window you make mywindow.title("New Project") #This will be the window title mywindow.geometry("780x640") #This will be the window size (str) mywindow.minsize(540, 420) #This will be set a limit for the window's minimum size (int) mywindow.configure(bg="blue") #This will be the background color mywindow.mainloop() #You must add this at the end to show the window |
Output:
Example 4: Basic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from tkinter import Tk, Label, Button class MyFirstGUI: def __init__(self, master): self.master = master master.title("A simple GUI") self.label = Label(master, text="This is our first GUI!") self.label.pack() self.greet_button = Button(master, text="Greet", command=self.greet) self.greet_button.pack() self.close_button = Button(master, text="Close", command=master.quit) self.close_button.pack() def greet(self): print("Greetings!") root = Tk() my_gui = MyFirstGUI(root) root.mainloop() |
Output: