tkinter code 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: make a window tkinter
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()
# Code to add widgets will go here...
top.mainloop()
Example 4: gui in tkinter
from tkinter import *
import os
# window
window = Tk()
window.geometry("450x450")
window.title("Gui App")
window.configure(bg="powder blue")
# Enter or user input in tkinter
filename = Entry(window, width=75)
filename.pack()
# Run file Function
def runFile():
try:
os.startfile(filename.get())
except:
error = Label(window, text=f"No file found as {filename.get}")
error.pack()
# Run file button
open_file_button = Button(window, text="Run File", command=runFile)
open_file_button.pack()
window.mainloop()
Example 5: how to create tkinter window
import tkinter
master = tkinter.Tk()
master.mainloop()
Example 6: tkinter code examples
from tkinter import *
window=Tk()
# add widgets here
window.title('Hello Python')
window.geometry("300x200+10+20")
window.mainloop()