Tkinter Notes

from tkinter import *
# from tkinter.ttk import *
# from tkinter import ttk
# from tkinter import scrolledtext
# from tkinter import messagebox
window = Tk()
# window.maxsize(450, 450) # width, height
window.title('First GUI')
# window.iconbitmap('path/name.ico') # to aadd icon on top left
# window.configure(background="colour/photo") # To set background color of window
# Setting window size (widthxheight)
window.geometry('1000x500')

'''WINDOWS'''
# name = Toplevel() # to create a extra window
# name.destroy = fuction to add that window
# window.iconify() or window.state('iconic') ,To minimize a window in TaskBar
# window.state('zoomed') To Maximize a window

'''LABEL'''
# Creating a label and setting its position using grid
# Changing Label's font
# lbl = Label(window, text='hello', font=('Arial Bold', 30))
# lbl.grid(column=0, row=0)
# Handling button click event
# def clicked():
# text = txt.get()
# lbl.configure(text=text)
# Now add command=func_name as arg in button


'''BUTTON'''
# Adding a Button
# Changing button foreground and background colors
# btn = Button(window, text='Click me',command=clicked, bg='red', fg='yellow')
# btn.grid(column=2, row=0)
# activebackground=Background color when the button is under the cursor.
# activeforeground=Foreground color when the button is under the cursor.

'''TEXTBOX/ENTRY'''
# Creating a textbox(input)
# Use txt.get() func to get the entered text(used un clickedFunc)
# use txt.focus() to write in it from anywhere
# arg=(state='disabled') to make it unusable
# txt = Entry(window, width=30)
# txt.grid(column=1, row=0)

'''COMBOBOX/DROPBOX'''
# Adding a combobox(options in drop)
# First from tkinter.ttk import *
# combo = Combobox(window)
# # Adding values in combobox
# combo['values'] = (1,2,3,4,5, "Text")
# # Select the current item for it
# combo.current(0) # It takes in index(0,1,2,..)
# combo.grid(column=3, row=0)
# combo.get() # To get the current item selected

'''DROPDOWN'''
# var = StringVar()
# drop = OptionMenu(window, var, 'Monday', 'Tuesday')
# drop.pack()

'''SCROLL_BAR'''
# scroll = Scrollbar(TextArea)
# scroll.pack(side=RIGHT, fill=Y)
# scroll.config(command=Widget.yview)
# Widget.config(yscrollcommand=scroll.set)

'''CHECKBUTTON'''
# set the checked state
# 1)
# chk_state = BooleanVar()
# chk_state.set(True) #check
# 2)
# chk_state = IntVar()
# chk_state.set(0) #uncheck
# chk_state.set(1) #check
# Adding a CheckButton
# chk =Checkbutton(window, text='choose', var=chk_state)
# chk.grid(column=0, row=0)
# chk.toggle() to toggle selection of checkbox
# To get the value of chk use chk_state.get() , it will print 0 and 1 if using Intvar

'''RADIOBUTTON'''
# def selected(): # command=selected
# pass
# selected = IntVar()
# rad1 = Radiobutton(window, value=1, text='one', variable=selected)
# rad2 = Radiobutton(window, value=2, text='two', variable=selected)
# rad3 = Radiobutton(window, value=3, text='three', variable=selected)
# rad1.grid(column=1, row=0)
# rad2.grid(column=2, row=0)
# rad3.grid(column=3, row=0)
# To get the value use selected.get() will give 1,2,3(value)

'''SCROLLED TEXT'''
# from tkinter import scrolledtext
# txt.insert(INSERT,'You text goes here')
# txt.delete(1.0,END) start,end
# txt = scrolledtext.ScrolledText(window,width=40,height=10)
# txt.grid(column=0,row=0)

'''MESSAGE BOX'''
# from tkinter import messagebox
# messagebox.showinfo('Message title','Message content')
# messagebox.showwarning('Message title', 'Message content') #shows warning message
# messagebox.showerror('Message title', 'Message content') #shows error message
# res = messagebox.askquestion('Message title','Message content')
# res = messagebox.askyesno('Message title','Message content')
# res = messagebox.askyesnocancel('Message title','Message content')
# res = messagebox.askokcancel('Message title','Message content')
# res = messagebox.askretrycancel('Message title','Message content')
# If you click OK or yes or retry, it will return True value, but if you choose no or cancel, it will return False.
# The only function that returns one of three values is askyesnocancel function, it returns True or False or None.

'''SPINBOX'''
# spin = Spinbox(window, from_=1, to_=100, width=5)
# spin.grid(row=0, column=0)
#
# You can specify the numbers for the Spinbox instead of using the whole range like this:
# spin = Spinbox(window, values=(3, 8, 11), width=5)
#
# Set the default value
# var = IntVar()
# var.set(7)
# spin = Spinbox(window, from_=0, to_=10, width=10, textvariable=var)
# spin.grid(row=0, column=0)

'''PROGRESS BAR'''
# from tkinter.ttk import Progressbar
# progress = Progressbar(window, length=200, value=10) # value is from 0 to 100(%)
# progress.grid(row=0, column=0)
# To change progressbar colour
# from tkinter import ttk
# style = ttk.Style()
# style.theme_use('default')
# style.configure("black.Horizontal.TProgressbar", background='black') # black = colour name
# bar = Progressbar(window, length=200, value=85,style='black.Horizontal.TProgressbar')
# bar.grid(row=0, column=0)

'''File dialog'''
from tkinter import filedialog
file = filedialog.asksaveasfilename()
# file = filedialog.askopenfilename()
# print(file)
#
# Specify file types (filter file extensions)
# file = filedialog.askopenfilename(filetypes = (("Text files","*.txt"),("all files","*.*")))
#
# You can specify the initial directory for the file dialog by specifying the initialdir like this:
# from os import path
# file = filedialog.askopenfilename(initialdir= path.dirname(__file__)) # dirname = variable
#
# You can ask for a directory using askdirectory method:
# dir = filedialog.askdirectory() # directory = folder
# After you choose a file and click open, the file variable will hold that file path.

'''MENU BAR'''
# from tkinter import Menu
# menu = Menu(window)
# menu.add_command(label='File')
# add menu items under any menu by using add_cascade()
# new = Menu(menu)
# new.add_command(label="New") # command=func_name
# new.add_separator() # separator is a horizontal line
# new.add_command(label='Edit')
# To disable the dashed line in beginning
# new = Menu(menu, tearoff=0)
# menu.add_cascade(label='File', menu=new)
# Add this in the end
# window.config(menu=menu)

'''Photo'''
# image = PhotoImage(file='download.png')
# lbl = Label(image=image).grid(row=0, column=0)

'''SLIDERS'''
# slider2 = Scale(window, from_=0, to=100, tickinterval=10, length=499)
# To change the length of slider sliderlength=
# To make it horizontal orient=HORIZONTAL
# DoubleVar(), Float variable
# slider2.set(20) # To set initial value of slider
# slider2.pack()

# EVENTS and BINDS

window.mainloop()

Comments