Created
June 12, 2018 11:18
-
-
Save bhaskar-nair2/8878d0f6113683a69c6705702b517742 to your computer and use it in GitHub Desktop.
tkinter application to run processes without getting blocked or hung
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
The aim of this Gist is to give you a base application where you just need to add your own elements/processes and make it work. | |
''' | |
import tkinter as tk | |
from tkinter import ttk,messagebox | |
import threading | |
import time | |
#base GUI Class | |
class GUI: | |
def __init__(self, root, runCommand): | |
mf = ttk.Frame(root, padding="5 5 5 5") | |
mf.grid(column=0, row=0) | |
mf.columnconfigure(0, weight=1) | |
mf.rowconfigure(0, weight=1) | |
# Global Values | |
self.Fnm = tk.StringVar(root, "SearchFile.xlsx") | |
self.Ncol = tk.StringVar(root, "D") | |
self.Vcol = tk.StringVar(root, "C") | |
# Label | |
tk.Label(mf, text="File Name").grid(column=1, row=1, pady=6) | |
tk.Label(mf, text="Name Col").grid(column=1, row=3, pady=6) | |
tk.Label(mf, text="Value Col").grid(column=3, row=3, pady=6) | |
# components | |
self.fname = ttk.Entry(mf, width=18, textvariable=self.Fnm) | |
self.nmCol = ttk.Entry(mf, width=6, textvariable=self.Ncol) | |
self.valCol = ttk.Entry(mf, width=6, textvariable=self.Vcol) | |
self.but = ttk.Button(mf, text="Refresh", command=runCommand) | |
self.pgbar = ttk.Progressbar(mf, orient="horizontal", mode="determinate") | |
# Design | |
self.fname.grid(column=2, row=1, pady=3, columnspan=3) | |
self.nmCol.grid(column=2, row=3, pady=3) | |
self.valCol.grid(column=4, row=3, pady=3) | |
self.but.grid(column=2, row=2, columnspan=2) | |
self.pgbar.grid(column=1,row=4,columnspan=4) | |
def refresh(self): | |
pass | |
def get(self): | |
return [self.Fnm.get(), self.Ncol.get(), self.Vcol.get()] | |
#Base process Class | |
class Proc: | |
def __init__(self, dets,pgbar,but): | |
self.Fnm = dets[0] | |
self.Ncol = dets[1] | |
self.Vcol = dets[2] | |
self.pg=pgbar | |
self.butt=but | |
def refresh(self): | |
self.butt['state'] = 'disabled' | |
self.pg.start() | |
#ATTENTION:Enter Your process Code HERE | |
for _ in range(5): | |
time.sleep(2) | |
self.pg.stop() | |
#Any search/sort algorithm to be used | |
#You can use self.pg.step() to be more specific for how the progress bar proceeds | |
messagebox.showinfo("Process Done","Success") | |
self.butt['state'] = 'enabled' | |
#Base Application Class | |
class App: | |
def __init__(self, master): | |
self.master = master | |
self.gui = GUI(self.master, self.runit) | |
def runit(self): | |
self.search = Proc(self.gui.get(),self.gui.pgbar,self.gui.but) | |
self.thread1 = threading.Thread(target=self.search.refresh) | |
self.thread1.start() | |
def main(): | |
app = tk.Tk() | |
gui = App(app) | |
app.title("Refresh Search File") | |
app.mainloop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment