I am new to tkinter GUI programming. I tried to search my problem, but I was unable to formulate the correct question without a description of my problem.
I designed a small GUI - for the example here - with a button and a ScrolledText item.
import tkinter as tk
from tkinter import messagebox as msg
from tkinter.ttk import Notebook
from tkinter import filedialog
import tkinter.scrolledtext as tkscrolled
import do_something as ds
import os
import time
class Fatt(tk.Tk):
def __init__(self):
super().__init__()
# window setup
self.title("Test Gui")
self.geometry("1024x768")
self.resizable(0, 0)
# tab
self.notebook = Notebook(self)
# define tabs
res_avg_tab = tk.Frame(self.notebook)
# group nodal averaging
group_avg = tk.LabelFrame(res_avg_tab, text="Perform nodal averaging of all selected DB files")
group_avg.pack(padx=10, pady=10)
# nodal averaging button
self.avg_button = tk.Button(group_avg, text="Perform Nodal Averaging",
command=self.nodal_avg, bg="lightgrey", fg="black", width=50)
self.avg_button.pack(side=tk.TOP, pady=10, padx=10)
# scrolled log-text window
# group LOG
group_log = tk.LabelFrame(res_avg_tab, text="Result Averaging Output (LOG)")
group_log.pack(padx=10, pady=10)
self.avg_log = tkscrolled.ScrolledText(group_log, bg="white", fg="black", height=13, width=110)
self.avg_log.pack(side=tk.TOP, fill=tk.X, padx=10, pady=10)
# status-bar
self.status_text = tk.StringVar(res_avg_tab)
self.status_text.set("---")
self.status = tk.Label(res_avg_tab, textvar=self.status_text,
bd=1, relief=tk.SUNKEN, anchor=tk.W)
self.status.pack(side=tk.BOTTOM, fill=tk.BOTH)
# add everything to tabs
self.notebook.add(res_avg_tab, text="Average Results")
self.notebook.pack(fill=tk.BOTH, expand=True)
def show_cmb_file_creator(self):
pass
def nodal_avg(self):
sel_dbs = ["file1", "file2", "file3"]
# write file-list to log-window
self.avg_log.insert(tk.INSERT, "\nSelected Files for Nodal Averaging:\n")
for i in sel_dbs:
self.avg_log.insert(tk.INSERT, i+'\n')
self.avg_log.see(tk.END)
# if yes --> average results
if msg.askyesno("Nodal Averaging", "Perform nodal averaging with selected db-results?"):
start = time.time()
self.status_text.set("processing...")
self.config(cursor="wait")
self.avg_log.insert(tk.INSERT, "Start nodal averaging - this may take some time...\n")
class_obj = ds.DoSomething(i, self.avg_log)
for i in sel_dbs:
class_obj.do_something()
end = time.time()
overall_time_str = " Overall Averaging RUNTIME: {0:.2f} sec ({1:.1f} min) ".format(end-start, (end-start)/60.0)
self.avg_log.insert(tk.INSERT, "\n{0:*^80}".format(overall_time_str))
self.avg_log.see(tk.END)
self.status_text.set("---")
self.config(cursor="")
def browse_dir(self):
pass
def copy_to_clipboard(self, text=None):
pass
if __name__=="__main__":
fatt = Fatt()
fatt.mainloop()
The button "avg_button" executes the function "nodal_avg" and the main purpose of this function is to instantiate an external class and run a method.
class_obj = ds.DoSomething(i, self.avg_log)
for i in sel_dbs:
class_obj.do_something()
This class contains the main logic of my software and it contains a lot of print outputs.
import tkinter.scrolledtext as tkscrolled
class DoSomething:
def __init__(self, my_file, outp_print=print):
self.my_file = my_file
self.outp_print = outp_print
# my-print function
# for tkinter-log output
def myprint(self, text):
if self.outp_print==print:
print(text)
elif isinstance(self.outp_print, tkscrolled.ScrolledText):
self.outp_print.insert("end", text+'\n')
else:
print("myprint - ERROR: {0}".format(str(self.outp_print)))
def do_something(self):
for i in range(0,100000):
self.myprint("{0:d} - printed output".format(i))
I would like to print the output of the class/method to the ScrolledText window, but I also like to maintain the classic print functionality. Therefore I use the "myprint" method - which is able to use print or ScrolledText.insert for printing (I do not know if this is a smart approach?!).
If I run the code it basically works - but the ScrolledText window does not update on every print, only when the method in the external class is finished - then the output appears.
So my question is - how can I continuously update my ScrolledText window with my output string?
Thank you very much.
Best Regards
Michael
Related
I am working on a python tkinter desktop applicaiton. I need a scrollbar on the right side of the frame with a vertical orientation. I am trying to display a ttk scrollbar but it does not seem to display properly. My table disappears and the height of the scrollbar is not correct either. Also, if possible, the scrollbar needs to appear only when the TreeView overflows and when it doesnt overflow, then the scrollbar should not be displayed.
import tkinter
from turtle import color, width
import win32com.client
import sys
import subprocess
import time
from tkinter import*
from tkinter import ttk
from tkinter import messagebox
class TestingGui():
def __init__(self):
print("testing")
def registerUser(self):
userName = "tim"
userAge = "36"
userGender = "male"
userPosition = "softeware engineer"
userInfo = [userName.upper(),userAge,userGender,userPosition]
tree.column(0,anchor='center')
tree.column(1,anchor='center')
tree.column(2,anchor='center')
tree.column(3,anchor='center')
tree.insert('',0,values=userInfo)
if __name__ == '__main__':
window = Tk()
window.title('Dashboard')
window.geometry('925x500+300+200')
window.configure(bg="#fff")
window.resizable(False,False)
################### Frame (Top)[start] #####################################
frameTop = Frame(window,width=860,height=60,bg='white')
frameTop.place(x=40,y=40)
uploadExcelBtn = Button(frameTop,width=19,pady=7,text='Upload Excel',bg='#787c82',fg='white',cursor='hand2',border=0).place(x=715,y=13)
excelFileInputField = Entry(frameTop,width=58,fg='black',border=1,bg='white',font=('Microsoft YaHei UI Light',15,'bold'))
excelFileInputField.place(x=8,y=14)
################### Frame (Top)[end] #######################################
################### Table (Center)[start] #####################################
columns = ('name','age','gender','position')
frameCenter = Frame(window,width=860,height=315,bg='#f0f0f1')
frameCenter.place(x=40,y=110)
treeScroll = ttk.Scrollbar(frameCenter,orient="vertical")
treeScroll.pack(side=RIGHT,fill="y")
tree = ttk.Treeview(frameCenter,height=13,columns=columns,show="headings",selectmode='browse',yscrollcommand=treeScroll.set)
tree.heading('name',text='Name')
tree.heading('age',text='Age')
tree.heading('gender',text='Gender')
tree.heading('position',text='Position')
tree.place(x=30,y=10)
treeScroll.config(command=tree.yview)
################### Table (Center)[end] #######################################
################### Frame (Bottom)[start] #####################################
frameBottom = Frame(window,width=860,height=60,bg='white')
frameBottom.place(x=40,y=430)
addUserBtn = Button(frameBottom,width=19,pady=7,text='Add User',bg='#57a1f8',fg='white',cursor='hand2',border=0,command= lambda : TestingGui().registerUser()).place(x=30,y=15)
################### Frame (Bottom)[end] #######################################
mainloop()
The use of place can be tricky that's why you should use it only if other geometrymanager fail to achieve what you want. The benefit of the other two geometrymanagers by tkinter is that they calculate cells or parcels for you that you can use and easily recognize by just look at your layout.
I took the time to change your script and placed comments next to changed lines, that should explain what and why I think those changes are necessary.
#import only what you need and avoid wildcard imports due naming conflicts
import tkinter as tk #as tk as short for tkinter
from tkinter import ttk
class TestingGui():
def __init__(self):
print("testing")
def registerUser(self):
userName = "tim"
userAge = "36"
userGender = "male"
userPosition = "softeware engineer"
userInfo = [userName.upper(),userAge,userGender,userPosition]
tree.column(0,anchor='center')
tree.column(1,anchor='center')
tree.column(2,anchor='center')
tree.column(3,anchor='center')
tree.insert('',0,values=userInfo)
if __name__ == '__main__':
window = tk.Tk()
## bonus: leading tk. symbols you are using tk
window.title('Dashboard')
window.geometry('925x500+300+200')
window.configure(bg="#fff")
window.resizable(False,False)
#Window Content
topframe = tk.Frame(window,width=860,height=60,bg='white')
centerframe = tk.Frame(window,width=860,height=315,bg='#f0f0f1')
bottomframe = tk.Frame(window,width=860,height=60,bg='white')
topframe.pack(side=tk.TOP, padx=(40,0),pady=(40,0),fill=tk.X)
centerframe.pack(side = tk.TOP, fill= tk.BOTH, padx=(40,0))
bottomframe.pack(side=tk.BOTTOM, padx=(40,0), fill= tk.X)
## fill = stretch in master
## padx/y are offsets like x/y in place but using their parcels
## keeping content together helps for an overview of content
#frameTop Content
input_field = tk.Entry(
topframe, width=58, fg='black', border=1, bg='white',
font=('Microsoft YaHei UI Light',15,'bold'))
input_field.pack(side=tk.LEFT)
upload_button = tk.Button(
topframe, width=19, pady=7, text='Upload Excel',
bg='#787c82',fg='white',cursor='hand2',border=0)
upload_button.pack(side=tk.TOP)
## seperate the constructor from the geometry method to keep a reference
## split lines for readability compare PEP-8 Style Guide
## dont use camelcase variable names compare PEP-8 Style Guide
## the order of packing matters in this geometry manager
#centerframe Content
treeScroll = ttk.Scrollbar(centerframe,orient="vertical")
treeScroll.pack(side=tk.RIGHT,fill="y")
#tk.RIGHT = tkinter constant
columns = ('name','age','gender','position')
tree = ttk.Treeview(
centerframe, height=13, columns=columns,
show="headings",selectmode='browse',yscrollcommand=treeScroll.set)
tree.heading('name',text='Name')
tree.heading('age',text='Age')
tree.heading('gender',text='Gender')
tree.heading('position',text='Position')
tree.pack(side=tk.TOP, padx=(30,0),pady=10)
treeScroll.config(command=tree.yview)
#bottomframe Content
add_user_button = tk.Button(
bottomframe, width=19, pady=7,text='Add User',
bg='#57a1f8',fg='white',cursor='hand2', border=0)
#command= lambda : TestingGui().registerUser())#DONT DO THIS!
## Only use lambda if needed
## It makes little to no sense to initiate a class in a lambda expression
add_user_button.pack(padx=(30,0),side=tk.LEFT)
#start mainloop
tk.mainloop()#indention correcture
I'm writing an app using tkinter for the GUI, and I want an indeterminate progress bar to be going back and forth while the main function is running, which sometimes takes a few seconds, depending on user input. Normally, the whole program freeze while the main function is running, so I am trying to establish a threaded process for the progress bar so it moves while main() is doing its thing (both functions are called withinscan_and_display()).
from tkinter import filedialog
from tkinter import *
from tkinter.ttk import Progressbar
from main import main
import graphs
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from PIL import ImageTk, Image
import threading
root = Tk()
launch_frame = Frame(root)
button_frame = Frame(root)
graph_frame = Frame(root, height=1000, width=1200)
# log directory button/text
logDirButton = Button(master=launch_frame, text='Select log storage location...',
command=lambda: get_directory(log_text), width=22)
log_text = Text(master=launch_frame, height=1, width=25)
logDirButton.grid(row=1, column=0)
log_text.grid(row=1, column=1)
# scan directory button/text
dirButton = Button(master=launch_frame, text="Select scan directory...", command=lambda: get_directory(t), width=22)
t = Text(master=launch_frame, height=1, width=25)
dirButton.grid(row=2, column=0)
t.grid(row=2, column=1)
# main scan button
mainButton = Button(master=launch_frame, text="SCAN!", state=DISABLED,
width=50, height=10, bg='#27b355')
mainButton.grid(row=3, column=0, columnspan=2)
# progress bar
progress = Progressbar(launch_frame, orient=HORIZONTAL, length=100, mode='indeterminate')
progress.grid(row=4, column=0, columnspan=2)
launch_frame.grid(row=0, column=0, sticky=NW)
def get_directory(text):
# first clear form if it already has text
try:
text.delete("1.0", END)
except AttributeError:
pass
directory = filedialog.askdirectory()
# store the first directory for later specific reference
text.insert(END, directory)
# disable scan button until user has given necessary info to run (log storage location, scan directory)
enable_scan_button(log_text, t)
return directory
def enable_scan_button(logText, dirText):
if logText.get("1.0", END) != '\n' and dirText.get('1.0', END) != '\n':
mainButton['state'] = NORMAL
mainButton['command'] = lambda: scan_and_display()
else:
mainButton['state'] = DISABLED
def scan_and_display():
threading.Thread(target=bar_start).start()
# get scan directory and log directory from text fields
log_directory = log_text.get("1.0", END)[:-1]
scan_directory = t.get("1.0", END)[:-1]
# store the initial scan directory for later reference
top_dir = scan_directory
# runs the main scan function. Passes scan_directory and log_directory arguments
data, scanDate = main(log_directory, scan_directory)
display(scan_directory, data, scanDate, top_dir)
def bar_start():
print("BAR START CALLED")
progress.start(10)
With this setup (and various other configurations I've trieD), the bar still freezes while main() is doing it's thing, and I need it to move to indicate to the user that something is happening.
You need to thread the long-running function, not the progressbar.
def scan_and_display():
# get scan directory and log directory from text fields
# It's best to do all tkinter calls in the main thread
log_directory = log_text.get("1.0", END)[:-1]
scan_directory = t.get("1.0", END)[:-1]
th = threading.Thread(target=bar_start, args=(log_directory, scan_directory))
th.start()
progress.start()
def bar_start(log_directory, scan_directory):
print("BAR START CALLED")
# store the initial scan directory for later reference
top_dir = scan_directory
# runs the main scan function. Passes scan_directory and log_directory arguments
data, scanDate = main(log_directory, scan_directory)
display(scan_directory, data, scanDate, top_dir)
progress.stop()
EDIT: here's a MCVE:
import tkinter as tk
from tkinter.ttk import Progressbar
import time
from threading import Thread
def long_running_function(arg1, arg2, result_obj):
"""accept some type of object to store the result in"""
time.sleep(3) # long running function
result_obj.append(f'DONE at {int(time.time())}')
root.event_generate("<<LongRunningFunctionDone>>") # trigger GUI event
def long_func_start():
x = 'spam'
y = 'eggs'
t = Thread(target=long_running_function, args=(x,y,data))
t.start()
result.config(text='awaiting result')
progress.start()
def long_func_end(event=None):
progress.stop()
result.config(text=f"process finished with result:\n{data[-1]}")
root = tk.Tk()
root.geometry('200x200')
btn = tk.Button(root, text='start long function', command=long_func_start)
btn.pack()
progress = Progressbar(root, orient=tk.HORIZONTAL, length=100, mode='indeterminate')
progress.pack()
result = tk.Label(root, text='---')
result.pack()
root.bind("<<LongRunningFunctionDone>>", long_func_end) # tell GUI what to do when thread ends
data = [] # something to store data
root.mainloop()
For homework, I have to create an application that creates a text field everytime a user clicks a button, and then get values from the fields when "submit" button is pressed.
The trace method shows up repeatedly, but I do not know how to use it. I know it requires a callback function, but what should that callback function be?
from tkinter import *
from tkinter import ttk
import sqlite3
import getpass
import wipComingIn
class Application(object):
def __init__(self,master):
self.master=master
self.ScanWIPIn = Button(master, text="Scan WIP In", width=25,
font='Calibri 12
bold',background='snow',command=self.scanWIPIn).grid(row=0, column=0,
padx=10)
def scanWIPIn(self):
incomingInventory=wipComingIn.scanIn()
def main():
root = Tk()
app=Application(root)
root.title("Main Menu")
root.configure(background="light cyan")
root.resizable(0, 0)
root.geometry('230x230+300+80')
root.mainloop()
if __name__=='__main__':
main()
class scanIn(Toplevel):
def __init__(self):
Toplevel.__init__(self)
self.geometry('300x100+350+100')
self.title('Scan In')
self.resizable(0,0)
self.num_rows=1
self.LocationLb = Label(self,text='Scan Location:',font='Arial
12').grid(row=1,column=1)
self.LocationBCText = Entry(self).grid(row=1,column=2)
self.AddLotBtn= Button(self,text="Scan
Lot",command=self.addField).grid(row=2,column=1)
self.CompleteTransaction =
Button(self,text="Complete",command=self.AddEntry).grid(row=2,column=4)
global listOfLots
listOfLots=[]
listOfLocation=[]
global rowNum
rowNum=2
def addField(self):
height =Toplevel.winfo_height(self)
height=height+25
global rowNum
rowNum=rowNum+1
listOfLots.append(StringVar())
newLot = Entry(self, textvariable=listOfLots[rowNum - 2])
newLot.grid(row=rowNum,column=2, pady=1)
listOfLots.append(StringVar())
geometryText='300'+str(height)+'350+100'
print(geometryText)
self.geometry('300x'+str(height)+'+350+100')
newLot.focus_set()
You could try just making a class that does it, for example:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('200x200')
class EntryListWidget(ttk.Frame):
"""Widget that creates a column of entry boxes."""
def __init__(self, master):
super().__init__(master)
self.entries = []
def add_entry(self):
"""Creates a new entry box and keeps reference to respective variable."""
entry_var = tk.StringVar()
self.entries.append(entry_var)
ttk.Entry(self, textvariable=entry_var).pack()
def get_entries(self):
"""Gets each entrybox text and returns as list."""
return [entry.get() for entry in self.entries]
entry_widget = EntryListWidget(root)
entry_widget.pack()
# Buttons to control adding new entry and getting their values
ttk.Button(root, text='Add Entry', command=entry_widget.add_entry).pack()
ttk.Button(root, text='Get Entries', command=entry_widget.get_entries).pack()
root.mainloop()
Just using the variable classes and not trace; I actually wouldn't use trace in this situation because I believe trace uses the callback every time the variable changes and here you have a one time "submit" button that collects all the values. You could extend this class idea to get what you're looking to do I bet.
I would like to ask if anyone knows how to get out a variable from an Entry in Tkinter to be used in future calculation.
Let us assume that I want to create a prompt where the user needs to place two numbers in the two different Entry widgets.
These numbers are to be used in another script for calculation. How can I retrieve the values from the prompt created in Tkinter?
In my opinion, I would need to create a function with the code bellow and make it return the value from the Tkinter prompt. However, I cannot return the numbers because I'm destroying the root window. How can I get pass this, preferably without global variables.
Best Regards
from tkinter import *
from tkinter import ttk
#Start of window
root=Tk()
#title of the window
root.title('Title of the window')
def get_values():
values=[(),(value2.get())]
return values
# Creates a main frame on the window with the master being the root window
mainframe=ttk.Frame(root, width=500, height=300,borderwidth=5, relief="sunken")
mainframe.grid(sticky=(N, S, E, W))
###############################################################################
#
#
# Label of the first value
label1=ttk.Label(master=mainframe, text='First Value')
label1.grid(column=0,row=0)
# Label of the second value
label2=ttk.Label(master=mainframe, text='Second Value')
label2.grid(column=0,row=1)
###############################################################################
#
#
# Entry of the first value
strvar1 = StringVar()
value1 = ttk.Entry(mainframe, textvariable=strvar1)
value1.grid(column=1,row=0)
# Entry of the second value
strvar2 = StringVar()
value2 = ttk.Entry(mainframe, textvariable=strvar2)
value2.grid(column=1,row=1)
# Creates a simplle button widget on the mainframe
button1 = ttk.Button(mainframe, text='Collect', command=get_values)
button1.grid(column=2,row=1)
# Creates a simplle button widget on the mainframe
button2 = ttk.Button(mainframe, text='Exit', command=root.destroy)
button2.grid(column=2,row=2)
root.mainloop()
You use a class because the class instance and it's variables remain after tkinter exits.https://www.tutorialspoint.com/python/python_classes_objects.htm And you may want to reexamine some of your documentation requirements, i.e. when the statement is
"root.title('Title of the window')", adding the explanation "#title of the window" is just a waste of your time..
""" A simplified example
"""
import sys
if 3 == sys.version_info[0]: ## 3.X is default if dual system
import tkinter as tk ## Python 3.x
else:
import Tkinter as tk ## Python 2.x
class GetEntry():
def __init__(self, master):
self.master=master
self.entry_contents=None
self.e = tk.Entry(master)
self.e.grid(row=0, column=0)
self.e.focus_set()
tk.Button(master, text="get", width=10, bg="yellow",
command=self.callback).grid(row=10, column=0)
def callback(self):
""" get the contents of the Entry and exit
"""
self.entry_contents=self.e.get()
self.master.quit()
master = tk.Tk()
GE=GetEntry(master)
master.mainloop()
print("\n***** after tkinter exits, entered =", GE.entry_contents)
So, I have taken Curly Joe's example and made a function with the his sketch
The final result, for anyone wanting to use this as a template for a input dialog box:
def input_dlg():
import tkinter as tk
from tkinter import ttk
class GetEntry():
def __init__(self, master):
self.master=master
self.master.title('Input Dialog Box')
self.entry_contents=None
## Set point entries
# First point
self.point1 = ttk.Entry(master)
self.point1.grid(row=0, column=1)
self.point1.focus_set()
# Second point
self.point2 = ttk.Entry(master)
self.point2.grid(row=1, column=1)
self.point2.focus_set()
# labels
ttk.Label(text='First Point').grid(row=0, column=0)
ttk.Label(text='Second Point').grid(row=1, column=0)
ttk.Button(master, text="Done", width=10,command=self.callback).grid(row=5, column=2)
def callback(self):
""" get the contents of the Entries and exit the prompt"""
self.entry_contents=[self.point1.get(),self.point2.get()]
self.master.destroy()
master = tk.Tk()
GetPoints=GetEntry(master)
master.mainloop()
Points=GetPoints.entry_contents
return list(Points)
In python, functions are objects, as in get_values is an object.
Objects can have attributes.
Using these two, and the knowledge that we can't really return from a button command, we can instead attach an attribute to an already global object and simply use that as the return value.
Example with button
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def on_button_press(entry):
on_button_press.value = entry.get()
entry.quit()
def main():
root = tk.Tk()
entry = tk.Entry(root)
tk.Button(root, text="Get Value!", command=lambda e = entry : on_button_press(e)).pack()
entry.pack()
tk.mainloop()
return on_button_press.value
if __name__ == '__main__':
val = main()
print(val)
Minimalistic example
Similarly modules are also objects, if you want to avoid occupying global namespace extremely, you can attach a new attribute to the module you're using
See:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
if __name__ == '__main__':
tk.my_value = lambda: [setattr(tk, 'my_value', entry.get()), root.destroy()]
root = tk.Tk()
entry = tk.Entry(root)
root.protocol('WM_DELETE_WINDOW', tk.my_value)
entry.pack()
tk.mainloop()
print(tk.my_value)
I have a Tkinter GUI having 2 entry fields, 2 buttons ( initialization of these not shown in code). There is one more button (initialized in code) which performs the main task of performing change detection on two images. Also there is a progress bar.
Now, when the task of change detection has been completed, I want to display the 4 images(pre, post, aligned, chng) returned by wave.changedetection() in a separate Tkinter window. I want the new window to come only after changedetection() has completed.(wave.py is my own file, not some module)
Unfortunately, if I try to add code to make new window, Tk.Toplevel() ,after the wave.changedetection() call, nothing happens and the main GUI window becomes unresponsive and has to be killed.
There is no way to know when the new created thread (start_thread)completes it's work, so that I can do Tk.Toplevel() there.
How can I do what I require?
class GUI(Tkinter.Tk):
def __init__(self, parent)
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.button = Tkinter.Button(text = "Start")
self.button.bind('<Button-1>', self.OnButtonClick)
self.button.pack()
self.int = Tkinter.IntVar()
self.pgbar = Tkinter.ProgressBar(variable = self.int, mode = determinate)
def OnButtonClick(self,event):
#this func has been made since I have more buttons.It may seem redundant here
self.button['command'] = self.start_thread()
self.update_idletasks()
def start_thread(self):
self.int_var.set(1)
q = queue.Queue()
self.secondary_thread = threading.Thread(target = self.change)
self.secondary_thread.start()
self.after(50, self.check_queue, q)
def check_queue(self, q):
while True:
try:
x = wave.q.get_nowait()
except queue.Empty :
self.after(50,self.check_queue,q)
break
else:
self.int_var.set(x)
if x == 6:
self.button3['state'] = 'normal'
break
def change(self):
'''The 6 functions of wave.changedetection() change the value of self.int
due to which progress bar progresses.'''
pre, post, aligned, chng = wave.changedetection(self.entry_1.get(),
self.entry_2.get())
if __name__ == '__main__':
gui = GUI(None)
gui.mainloop()
code to update progress bar taken from here (2nd answer,Honest Abe's answer)
You have to be able to differentiate name spaces, i.e. this is in the main window and this is in the Toplevel. I would suggest that you get the Toplevels working first and then decide if you want to add threading or not. The code below is a simple example of creating Toplevels and shows how to place widgets in a specific name space (window in this case). You may or may not want a separate "create a Toplevel" class if there are functions you want to associate with each Toplevel's namespace. Also there are examples on the web on using Tkinter's "after" to update a progressbar. That is a different question so start another thread if you have questions about the progressbar.
try:
import Tkinter as tk ## Python 2.x
except ImportError:
import tkinter as tk ## Python 3.x
from functools import partial
class OpenToplevels():
""" open and close additional Toplevels with a button
"""
def __init__(self):
self.root = tk.Tk()
self.button_ctr=0
## in the "root" namespace *********************
but=tk.Button(self.root, text="Open a Toplevel",
command=self.open_another)
but.grid(row=0, column=0)
tk.Button(self.root, text="Exit Tkinter", bg="red",
command=self.root.quit).grid(row=1, column=0, sticky="we")
self.root.mainloop()
def close_it(self, id):
## destroy the window in this id's namespace ***********
id.destroy()
## id.withdraw()
## id.iconify()
def open_another(self):
self.button_ctr += 1
id = tk.Toplevel(self.root)
id.title("Toplevel #%d" % (self.button_ctr))
## in the "id for this Toplevel" namespace ***********
tk.Button(id, text="Close Toplevel #%d" % (self.button_ctr),
command=partial(self.close_it, id),
bg="orange", width=20).grid(row=1, column=0)
Ot=OpenToplevels()