Opening Hours :7AM to 9PM
from tkinter import * window = Tk() window.title("Welcome to Msk Tutorials") window.mainloop()
import tkinter as tk def msk(): print("welcome to msk tutorials") top = tk.Tk() frame = tk.Frame(top) frame.pack() button = tk.Button(frame, text="QUIT", fg="red", command=quit) button.pack(side=tk.LEFT) logan = tk.Button(frame, text="Hello", command=msk) logan.pack(side=tk.LEFT) top.mainloop()
from tkinter import * def showfields(): print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get())) master = Tk() Label(master, text="First Name").grid(row=0) Label(master, text="Last Name").grid(row=1) e1 = Entry(master) e2 = Entry(master) e1.grid(row=0, column=1) e2.grid(row=1, column=1) Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4) Button(master, text='Show', command=showfields).grid(row=3, column=1, sticky=W, pady=4) mainloop( )Check Box
from tkinter import * master = Tk() def states(): print("male: %d, \nfemale: %d" % (var1.get(), var2.get())) Label(master, text="Your Gender:").grid(row=0, sticky=W) var1 = IntVar() Checkbutton(master, text="male", variable=var1).grid(row=1, sticky=W) var2 = IntVar() Checkbutton(master, text="female", variable=var2).grid(row=2, sticky=W) Button(master, text='Quit', command=master.quit).grid(row=3, sticky=W, pady=4) Button(master, text='Show', command=states).grid(row=4, sticky=W, pady=4) mainloop()
Message Box
import tkinter as tk top = tk.Tk() msk = "welcome to msk tutorials" msg = tk.Message(top, text = msk) msg.config(bg='lightgreen', font=('times', 24, 'italic')) msg.pack() tk.mainloop()
Radio Buttion
import tkinter as tk root = tk.Tk() v = tk.IntVar() v.set(1) # initializing the choice, i.e. Python languages = [ ("Python",1), ("Perl",2), ("Java",3), ("C++",4), ("C",5) ] def ShowChoice(): print(v.get()) tk.Label(root, text="""Choose your favourite programming language:""", justify = tk.LEFT, padx = 20).pack() for val, language in enumerate(languages): tk.Radiobutton(root, text=language, padx = 20, variable=v, command=ShowChoice, value=val).pack(anchor=tk.W) root.mainloop()
Convas Designing Python GUI
from tkinter import * top= Tk() w = Canvas(top, width=200, height=100) w.pack() w.create_rectangle(50, 20, 150, 80, fill="#1863ae") w.create_rectangle(65, 35, 135, 65, fill="red") w.create_line(0, 0, 50, 20, fill="#476042", width=4) w.create_line(0, 100, 50, 80, fill="#476042", width=4) w.create_line(150,20, 200, 0, fill="#1863ae", width=4) w.create_line(150, 80, 200, 100, fill="#476042", width=4) mainloop()
Multiple Forms Handling
import tkinter as tk from tkinter import * from tkinter import ttk class Multipleforms1( Frame ): def __init__( self ): tk.Frame.__init__(self) self.pack() self.master.title("Msk Tutorials.com") self.button1 = Button( self, text = "CLICK HERE", width = 25, command = self.new_window ) self.button1.grid( row = 0, column = 1, columnspan = 2, sticky = W+E+N+S ) def new_window(self): self.newWindow = Multipleforms2() class Multipleforms2(Frame): def __init__(self): new =tk.Frame.__init__(self) new = Toplevel(self) new.title("www.Inihsrav.com") new.button = tk.Button( text = "PRESS TO CLOSE", width = 25, command = self.close_window ) new.button.pack() def close_window(self): self.destroy() def main(): Multipleforms1().mainloop() if __name__ == '__main__': main()
Create a label widget
from tkinter import * top = Tk() top.title("Welcome to Msk Tutorials") lbl = Label(top, text="Welcome ") lbl.grid(column=0, row=0)
Set label font size
from tkinter import * top = Tk() top.title("Welcome to Msk Tutorials") lbl = Label(top, text="Welcome", font=("Arial Bold", 50)) lbl.grid(column=0, row=0)
Button Click Example
from tkinter import * top = Tk() top.title("Welcome to Msk Tutorials") top.geometry('350x200') lbl = Label(top, text="Hello") lbl.grid(column=0, row=0) def clicked(): lbl.configure(text="Button was clicked !!") btn = Button(top, text="Click Me", command=clicked) btn.grid(column=1, row=0) top.mainloop()
Add a combobox widget
from tkinter import * from tkinter.ttk import * top = Tk() top.title("Welcome to Msk Tutorials") top.geometry('350x200') combo = Combobox(top) combo['values']= (1, 2, 3, 4, 5, "Text") combo.current(1) #set the selected item combo.grid(column=0, row=0) top.mainloop()
Add a Checkbutton widget (Tkinter checkbox)
from tkinter import * from tkinter.ttk import * top = Tk() top.title("Welcome to Msk Tutorials") top.geometry('300x200') chk_state = BooleanVar() chk_state.set(True) #set check state chk = Checkbutton(top, text='Choose', var=chk_state) chk.grid(column=0, row=0) top.mainloop()
Get radio button value (selected radio button)
from tkinter import * from tkinter.ttk import * top = Tk() top.title("Welcome to Msk Tutorials") selected = IntVar() radio1 = Radiobutton(top,text='First', value=1, variable=selected) radio2 = Radiobutton(top,text='Second', value=2, variable=selected) radio3 = Radiobutton(top,text='Third', value=3, variable=selected) def clicked(): print(selected.get()) btn = Button(top, text="Click Me", command=clicked) radio1.grid(column=0, row=0) radio2.grid(column=1, row=0) radio3.grid(column=2, row=0) btn.grid(column=3, row=0) top.mainloop()
Add a ScrolledText widget (Tkinter textarea)
from tkinter import * from tkinter import scrolledtext top = Tk() top.title("Welcome to Msk Tutorials") top.geometry('350x200') txt = scrolledtext.ScrolledText(top,width=40,height=10) txt.grid(column=0,row=0) top.mainloop()
Add a SpinBox (numbers widget)
from tkinter import * top = Tk() top.title("Welcome to Msk Tutorials") top.geometry('350x200') spin = Spinbox(top, from_=0, to=100, width=5) spin.grid(column=0,row=0) top.mainloop()
Add a Progressbar widget
from tkinter import * from tkinter.ttk import Progressbar from tkinter import ttk top = Tk() top.title("Welcome to Msk Tutorials") top.geometry('400x200') style = ttk.Style() style.theme_use('default') style.configure("blue.Horizontal.TProgressbar", background='blue') bar = Progressbar(top, length=200, style='blue.Horizontal.TProgressbar') bar['value'] = 80 bar.grid(column=0, row=0) top.mainloop()
Add a Menu bar
from tkinter import * from tkinter import Menu top = Tk() top.title("Welcome to LikeGeeks app") menu = Menu(top) new_item = Menu(menu) new_item1 = Menu(menu) new_item.add_command(label='New') new_item1.add_command(label='Copy') menu.add_cascade(label='File', menu=new_item) menu.add_cascade(label='Edit', menu=new_item1) top.config(menu=menu) top.mainloop()
Add a Notebook widget (tab control)
from tkinter import ttk from tkinter import * top = Tk() top.title("Welcome to Msk Tutorials") tab_control = ttk.Notebook(top) tab1 = ttk.Frame(tab_control) tab2 = ttk.Frame(tab_control) tab_control.add(tab1, text='First') tab_control.add(tab2, text='Second') lbl1 = Label(tab1, text= 'label1') lbl1.grid(column=0, row=0) lbl2 = Label(tab2, text= 'label2') lbl2.grid(column=0, row=0) tab_control.pack(expand=1, fill='both') top.mainloop()