python gui tutorial code example

Example 1: tkinter basic

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()

Example 2: how to create a tkinter window

#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

Example 3: python basic gui

from tkinter import *


# def click func
def click():
    # Getting the text info as an int() & Error handling
    try:
        text_info_1 = float(text1.get())
        text_info_2 = float(text2.get())
    except Exception as e:
        text1.delete(0, END)
        text2.delete(0, END)
        text3.delete(0, END)
        text3.insert(0, f'Error: {e}')
        return
    # actual part of the func
    text3.delete(0, END)
    text3.insert(0, text_info_1 + text_info_2)

# Gui Config
root = Tk()
root.geometry('300x400')
root.title('Poop')

# The actual gui
label1 = Label(root, text='Write something!')
label1.pack()

spacing1 = Label(root)
spacing1.pack()

text1 = Entry(root)
text1.pack(ipadx=20)

spacing2 = Label(root, text='+')
spacing2.pack()

text2 = Entry(root)
text2.pack(ipadx=20)

spacing3 = Label(root)
spacing3.pack()

button = Button(root, text='Click me!', command=click)
button.pack()

spacing4 = Label(root)
spacing4.pack()

text3 = Entry(root)
text3.pack(ipadx=60)

# Making the gui run
root.mainloop()

Example 4: basic tkinter gui

import tkinter as tk
root = tk.Tk()
root.title("my title")
root.geometry('200x150')
root.configure(background='black')

#	enter widgets here

root.mainloop()

Example 5: python gui

# App python gui

import tkinter as tk
import webbrowser as wb


def Facebook():
    wb.open('facebook.com')


def Instagram():
    wb.open('instagram.com')


def Twitter():
    wb.open('twitter.com')


def Youtube():
    wb.open('youtube.com')


def Google():
    wb.open('google.com')


window = tk.Tk()
window.title('Browser')

google = tk.Button(window, text='Google', command=Google)
youtube = tk.Button(window, text='Youtube', bg='red', fg='white', command=Youtube)
twitter = tk.Button(window, text='Twitter', bg='powder blue', fg='white', command=Twitter)
Instagram = tk.Button(window, text='Instagram', bg='white', fg='black', command=Instagram)
facebook = tk.Button(window, text='Facebook', bg='blue', fg='white', command=Facebook)
facebook.pack()
Instagram.pack()
twitter.pack()
youtube.pack()
google.pack()

window.mainloop()

Example 6: python gui

import tkinter as tk
#Importing the main module
window = tk.Tk()
window.mainloop()