Skip to content

TKINTER Module

from tkinter import *
from tkinter.ttk import *
import tkinter.filedialog


def submit():
    entered_text = entry.get() # get text from entry box
    out.delete(0.0, END) # 0.0 (from first line and char); END (upto the end)
    out.insert(END, entered_text)

# open file_selector window
def open_file():
    one_file = tkinter.filedialog.askopenfilename(filetypes =[('CSV Files', '*.csv')])
    # to make multiple files selectable use below
    files = askopenfilenames(initialdir='c:/', title="Select multiple files", filetypes=[('Zip Files', '*.zip')])


def close_window():
    window.destroy()
    exit()

## MAIN WINDOW
window = Tk()
window.title("Binary check automation")
window.configure(background="black")
window.geometry("400x600")
window.resizable(0, 0)

# Element's code 

window.mainloop()
Elements and codes |Element| Code| |:---|:---| |Text Label| `Label(window, text="hello there!", bg="black", fg="white", font=("Fira code", 18, "bold"))`| |Entry Box
Single line input box|`Entry(window, width=20, bg="white")`| |Text Box
Multiple line input box|`Text(window, width=50, height=6, wrap=WORD, background="white")`| |Button|`Button(window, text="SUBMIT", bg="#858585", width=6, command=submit, style='G.TButton)`| |Photo Image|`PhotoImage(file="testimg.gif")`
```Label(window, image=photo, bg="black").grid(row=0, column=0, sticky=W)```| |Label Frame|`LabelFrame(window, text="Square frame with text on top left corner inside border")`|

How to:

How to show messagebox?
from tkinter import messagebox

messagebox.showerror("message")
messagebox.showinfo("message")
messagebox.showwarning("message")
How to show menu?
menu = Menu(window) # Main menu object
window.config(menu=menu)

m1 = Menu(menu, tearoff=0)
m1.add_command(label='menu 1.1', command=menu_click_command)
m2 = Menu(menu, tearoff=0)
m2.add_command(label='menu 2.1')
m3 = Menu(menu, tearoff=0)
m3.add_command(label='menu 2.2')

# menu_1 and menu_2 will be two main menu items
menu.add_cascade(label='menu_1', menu=m1)
menu.add_cascade(label='menu_2', menu=m2)
menu.add_cascade(label='menu_2', menu=m3)
How to make table and add data, add scrollbar?
trv = Treeview(window, columns=(1, 2, 3), show="headings")
trv.pack(fill=BOTH, expand="yes", side=LEFT)
trv.column('#1', width=150, stretch=NO)
trv.column('#3', width=350, stretch=NO)
trv.heading(1, text="Column 1 Name")
trv.heading(2, text="Column 2 Name")
trv.heading(3, text="Column 3 Name")

trv.tag_configure('green', background='#81C784')
trv.tag_configure('red', background='#EF5350')
# what to happen when double clicked on any row
trv.bind("<Double-1>", lambda x: tree_double_click(trv))

y_scroll = Scrollbar(window, orient=VERTICAL)
y_scroll.pack(side=RIGHT, fill='y')
y_scroll.config(command=trv.yview)
trv.config(yscrollcommand=y_scroll.set)

# Add data into treeview
# this row will show green background
trv.insert("", END, values=('col_1_data', 'col_2_data', 'col_3_data'), tags=('green',))
# this row will show red background
trv.insert("", END, values=('col_1_data', 'col_2_data', 'col_3_data'), tags=('red',))