ttk.Progressbar being duplicated - python

I'm having a peculiar problem when displaying a widget containing a tkinter.StringVar variable.
This happens whether or not I use frames to locate the variable.
In brief, I have three non-variable labels, one variable label, a progressbar and two buttons all gridded vertically (the button are side by side though (irrelevant but supplied for completeness).
When the variable is changed programmatically, concatenating a new character ('\n) and more text, the variable shows the extra lines, but duplicate progressbars and buttons are displayed:
(Separate images here).
Interesting note: This does not happen if the '\n' is not added.
import os
import sys
import time
import tkinter as tk
import tkinter.ttk as ttk
class rtGetPaths(tk.Frame):
"""
"""
def __init__(self, root):
tk.Frame.__init__(self, root)
self.root = root
# Get the system separator
self.path_sep = os.pathsep
# Find the root on the drive
self.root_id = os.path.abspath(os.sep)
self.data = [(os.path.join('C:', 'Program Files', 'App_1.exe')),
(os.path.join('C:', 'Program Files', 'App_2.exe')),
(os.path.join('C:', 'Program Files', 'App_3.exe')),
(os.path.join('C:', 'Program Files', 'App_4.exe'))
]
self.locns = []
self.root.title('Get Paths')
self.gpw_l1 = tk.Label(self.root, text='Searching for apps')
self.gpw_l2 = tk.Label(self.root, text='This may take some time')
self.gpw_l3 = tk.Label(self.root, text='Please be patient')
self.gpw_found_l_svar = tk.StringVar()
self.gpw_found_l_svar.set('')
self.gpw_found_l = tk.Label(self.root, textvariable=self.gpw_found_l_svar)
self.gpw_pb_ivar = tk.IntVar()
self.gpw_pb_ivar.set(0)
self.gpw_pb_length = 350
self.gpw_pb_max = 5
self.gpw_pb = ttk.Progressbar(self.root,
mode='determinate',
maximum=self.gpw_pb_max,
length=self.gpw_pb_length,
variable=self.gpw_pb_ivar)
self.gpw_exit_b = tk.Button(self.root,
text='Exit',
command=sys.exit)
self.gpw_continue_b = tk.Button(self.root,
text='Continue',
command=self.gpw_action_continue_b)
row = 0
self.gpw_l1.grid(row=row, columnspan=2)
row += 1
self.gpw_l2.grid(row=row, columnspan=2)
row += 1
self.gpw_l3.grid(row=row, columnspan=2)
row += 1
self.gpw_found_l.grid(row=row, columnspan=2)
row += 1
self.gpw_pb.grid(row=row, columnspan=2)
row += 1
self.gpw_exit_b.grid(row=row, column=0, sticky='ew')
self.gpw_continue_b.grid(row=row, column=1, sticky='ew')
self.gpw_found_l.grid_remove()
self.root.geometry('+100+200')
def gpw_action_continue_b(self):
"""
:return:
"""
for file in self.data:
lookfor = ''
if 'App_1.exe' in file:
lookfor = file
elif 'App_2.exe' in file:
lookfor = file
elif 'App_3.exe' in file:
lookfor = file
elif 'App_4.exe' in file:
lookfor = file
if lookfor != '':
self.locns.append(lookfor)
label = self.gpw_found_l_svar.get()
if 0 < self.gpw_pb_ivar.get() < 5:
label = label + '\n'
label = label + os.path.join(lookfor)
self.gpw_found_l_svar.set(label)
self.gpw_pb_ivar.set(self.gpw_pb_ivar.get() + 1)
if not self.gpw_found_l.winfo_viewable():
self.gpw_found_l.grid()
self.root.update_idletasks()
time.sleep(1)
self.gpw_continue_b['state'] = 'disabled'
self.gpw_pb_ivar.set(self.gpw_pb_max)
self.root.update_idletasks()
return
#==============================================================================
# MAIN (MAIN)
#==============================================================================
def main():
""" Run the app
"""
# # Create the screen instance and name it
root = tk.Tk()
app = rtGetPaths(root)
root.mainloop()
root.quit()
#==============================================================================
# MAIN (STARTUP)
#==============================================================================
if __name__ == '__main__':
# Run the function name main()
main()
Why is it doing this, and how do I stop it?

I'm not sure why this problem is occurring, but you can fix it by updating the progress bar each time you change the lable
The code:
def gpw_action_continue_b(self):
for file in self.data:
...
self.gpw_pb.update() # this fixes the problem
self.root.update_idletasks()
time.sleep(1)
Proof it works:

Related

How do I make python tkinter widgets appear on the right side of a frame and grow left?

Current behavior, adding LabelFrame widgets (with their own label and picture children) into another Labelframe named "Test putting buffs..." The LabelFrame widgets are being added into the left side of the LabelFrame and "grow" to the right. Wanted behavior is for the widgets to appear on the right side of the frame and "grow" left.
Cannot seem to get there with anchor or sticky settings. How can this "grow-left" be done and still preserve the ability to sort by name or time remaining?
Current behavior gif:
Wanted behavior (mocked up with paint):
Code (took out the image stuff so files aren't needed to run):
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
import time
class MainFrame(tk.Frame):
def __init__(self, container):
super().__init__(container)
self.grid(column=0, row=0)
self.buffs_list_frames = []
self.button1 = ttk.Button(self)
self.button1['text'] = "Simulate frame list appended True Strike"
self.button1['command'] = lambda: self.buffs_frame_list_is_appended(["True Strike", time.time() + 9])
self.button1.grid(column=0, row=0)
self.button2 = ttk.Button(self)
self.button2['text'] = "Simulate frame list appended Bulls"
self.button2['command'] = lambda: self.buffs_frame_list_is_appended(["Bulls", time.time() + 1080])
self.button2.grid(column=0, row=1)
self.button0 = ttk.Button(self)
self.button0['text'] = "Simulate frame list appended Endurance"
self.button0['command'] = lambda: self.buffs_frame_list_is_appended(["Endurance", time.time() + 1080])
self.button0.grid(column=0, row=2)
self.button3 = ttk.Button(self)
self.button3['text'] = "Simulate frame list put into .grid() and displayed"
self.button3['command'] = lambda: self.buffs_display_nicely()
self.button3.grid(column=0, row=3)
self.button4 = ttk.Button(self)
self.button4['text'] = "START loops of time passing"
self.button4['command'] = lambda: self.buffs_loop_time_passing()
self.button4.grid(column=0, row=4)
self.test_label_frame = ttk.LabelFrame(self, text="Testing putting buffs into a frame with grid")
self.test_label_frame.grid(column=1, row=0)
def buffs_loop_time_passing(self):
for x in self.buffs_list_frames:
x.buff_timer.set(f"{x.buff_birthday - time.time():.1f}s")
if x.buff_birthday < time.time() + 6:
x['background'] = 'red'
if x.buff_birthday < time.time():
self.buffs_list_frames.remove(x)
x.destroy()
self.after(1000, self.buffs_loop_time_passing)
def buffs_frame_list_is_appended(self, added_buff):
""" makes the buff frame and adds to the list of frame widgets
"""
self.buff_frame = tk.LabelFrame(self.test_label_frame, borderwidth=1, text=added_buff[0][0:4], labelanchor="n")
# self.buff_frame.buff_image_reference = ImageTk.PhotoImage(Image.open(added_buff[2]))
# self.buff_frame.buff_image_label = ttk.Label(self.buff_frame, image=self.buff_frame.buff_image_reference)
# self.buff_frame.buff_image_label.image_keep = self.buff_frame.buff_image_reference
# self.buff_frame.buff_image_label.grid(column=0, row=0)
self.buff_frame.buff_birthday = added_buff[1]
self.buff_frame.buff_timer = tk.StringVar()
self.buff_frame.buff_timer.set(f"{self.buff_frame.buff_birthday - time.time():.1f}s")
self.buff_frame.buff_label = ttk.Label(self.buff_frame, textvariable=self.buff_frame.buff_timer)
self.buff_frame.buff_label.grid(column=0, row=1)
self.buffs_list_frames.append(self.buff_frame)
self.buffs_display_nicely()
def buffs_display_nicely(self):
""" takes the list of frames, sorts by name, and .grids()s them into the test frame
"""
self.buffs_list_frames = sorted(self.buffs_list_frames, key=lambda z: z['text'])
print(f"sorted? {self.buffs_list_frames}")
j = 0
for x in self.buffs_list_frames:
x.grid(row=0, column=j)
j += 1
class App(tk.Tk):
def __init__(self):
super().__init__()
# configure the root window
self.title('NWN Buff Watcher')
self.geometry('300x50')
if __name__ == "__main__":
app = App()
main_frame = MainFrame(app)
app.mainloop()
Try this (using #acw1668's suggestion):
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
import time
class MainFrame(tk.Frame):
def __init__(self, container):
super().__init__(container)
self.buffs_list_frames = []
self.buttons_frame = tk.Frame(self)
self.buttons_frame.pack(fill="both", side="left")
self.button1 = ttk.Button(self.buttons_frame, text="Simulate frame list appended True Strike",
command=lambda: self.buffs_frame_list_is_appended("True Strike", 9))
self.button1.grid(column=0, row=0)
self.button2 = ttk.Button(self.buttons_frame, text="Simulate frame list appended Bulls",
command=lambda: self.buffs_frame_list_is_appended("Bulls", 1080))
self.button2.grid(column=0, row=1)
self.button0 = ttk.Button(self.buttons_frame, text="Simulate frame list appended Endurance",
command=lambda: self.buffs_frame_list_is_appended("Endurance", 1080))
self.button0.grid(column=0, row=2)
#self.button3 = ttk.Button(self.buttons_frame, text="Order items", command=self.order_items)
#self.button3.grid(column=0, row=3)
self.button4 = ttk.Button(self.buttons_frame, text="START loops of time passing",
command=self.buffs_loop_time_passing)
self.button4.grid(column=0, row=4)
self.test_label_frame = ttk.LabelFrame(self, text="Testing putting buffs into a frame with grid")
self.test_label_frame.pack(side="right")
def buffs_loop_time_passing(self):
for x in self.buffs_list_frames:
x.buff_timer.set(f"{x.buff_birthday - time.time():.1f}s")
time_now = time.time()
if x.buff_birthday < time_now + 6:
x.config(bg="red")
if x.buff_birthday < time_now:
self.buffs_list_frames.remove(x)
x.destroy()
self.after(100, self.buffs_loop_time_passing)
def buffs_frame_list_is_appended(self, added_buff, time_alive):
""" makes the buff frame and adds to the list of frame widgets
"""
buff_frame = tk.LabelFrame(self.test_label_frame, borderwidth=1,
text=added_buff[:4], labelanchor="n")
# buff_frame.buff_image_reference = ImageTk.PhotoImage(Image.open(added_buff[2]), master=self)
# buff_frame.buff_image_label = ttk.Label(buff_frame, image=buff_frame.buff_image_reference)
# buff_frame.buff_image_label.image_keep = buff_frame.buff_image_reference
# buff_frame.buff_image_label.grid(column=0, row=0)
buff_frame.buff_birthday = time.time() + time_alive
buff_frame.buff_timer = tk.StringVar(master=self)
buff_frame.buff_timer.set(f"{buff_frame.buff_birthday - time.time():.1f}s")
buff_frame.buff_label = ttk.Label(buff_frame,
textvariable=buff_frame.buff_timer)
buff_frame.buff_label.grid(column=0, row=1)
self.buffs_list_frames.append(buff_frame)
self.order_items()
def order_items(self):
self.buffs_list_frames = sorted(self.buffs_list_frames, key=lambda z: z['text'], reverse=True)
for x in self.buffs_list_frames:
x.pack_forget()
x.pack(side="right")
class App(tk.Tk):
def __init__(self):
super().__init__()
# configure the root window
self.title("NWN Buff Watcher")
# self.geometry("300x50")
if __name__ == "__main__":
app = App()
main_frame = MainFrame(app)
main_frame.pack()
app.mainloop()
I made a few changes to your code. The main thing is that I removed buffs_display_nicely and instead added buff_frame.pack(side="right"). The side="right" tells tkinter to add the widgets from the right to the left.
Also you can improve your code by a lot if you add a class instead of using buff_frame.buff_birthday, buff_frame.buff_timer, ... Also it will make your life a lot easier

Python, I want user to input text file

Hello currently I'm working on a word occurrence counter, I created a gui for it where users can type the words and then press the "count" button and it will count the occurence of each word, however I want to make it so user can instead upload a text file and the word occurrence will count the occurence of each word in the text file instead. Does anyone know how to make the transition? I need to know how to change it from user input words to user upload text files.
import tkinter as tk
from tkinter import *
from collections import Counter
#Functions
def countWords(s):
signos = [',', '.', ';', ':']
cleanstr = ''
for letra in s.lower():
if letra in signos:
cleanstr += ''
else:
cleanstr += letra
strlist = cleanstr.split(' ')
return dict(Counter(strlist))
def button_count():
text = mainWindow.e2.get()
count = countWords(text)
myLabel = Label(root, text=count)
myLabel.pack()
#Graphics
root = tk.Tk()
root.title("Count words")
root.geometry('400x400')
#Background Image Label
bg = PhotoImage(file = "./guibackground.gif")
# Show image using label
label1 = Label( root, image = bg)
label1.place(relx=0.5, rely=0.5, anchor=CENTER)
#Class Window
class Window:
def __init__(self, root):
self.root = root
self.e2 = tk.StringVar()
self.e = tk.Entry(root, textvariable=self.e2, width=35, borderwidth=5)
self.e.pack()
self.button = Button(root, text="Count words", command=button_count)
self.button.pack()
self.exit_button = Button(root, text="Exit", command=root.quit)
self.exit_button.pack()
if __name__ == '__main__':
mainWindow = Window(root)
Use filedialog.askopenfilename method:
import tkinter as tk
from tkinter import filedialog
from collections import Counter
class App(object):
def __init__(self):
self.root = tk.Tk()
self.btn = tk.Button(text='Open File', command=self.open_file)
self.btn.pack()
self.lbl = tk.Label()
self.lbl.pack()
self.root.mainloop()
def open_file(self):
filename = filedialog.askopenfilename(initialdir='/', title='Select file', filetypes=(('text files','*.txt'), ('all files','*.*')))
with open(filename, 'r') as f:
self.lbl.configure(text=f'{Counter(f.read().split())}')
App()
Output:

Cannot close the active listbox window

I've created a function btnExit outside the class myWindow and a method inside the class with the same name. I have called each of these in numerous ways and with a number of recommendations to close the current window. Nothing works.
It's obvious from my code that I just learning Python and come from c++.
Two problems:
if I use command=self.btnExit, I get this error: object has no attribute 'btnExit'.
I can call command=btnExit the function is accessed, but nine of the functions I try close the window.
self.btn2 = tk.Button(self, text="EXIT", fg="red", command=btnExit)
Minimally functional code:
#!/usr/bin/python
#Cura json iteration reporter
import os
import sys
from tkinter import *
import tkinter as tk
root = Tk()
root.overrideredirect(1) #disable root window
root.withdraw()
#test data
display = [
" 0 . name = Extruder",
" 1 . version = 2",
" 2 . metadata",
" 3 . . type = extruder",
" 4 . . author = Ultimaker",
" 5 . . manufacturer = Unknown",
" 6 . . setting_version = 1",
" 7 . . visible = False",
" 8 . . position = 0",
" 9 . settings",
" 10 . . machine_settings"
]
line_cnt = 10
pathFilenameExt = "D:/Python/$my Projects/CURA json profiles examples/fdmextruder.def.json"
fileDialogTitle = "Cura profile files"
win_size = '500x500'
# !!!! - this btnExit outside of any class class
def btnExit():
choice = messagebox.askquestion("Exit Program?", "Are you sure?", icon='warning')
if choice == 'yes':
#???what goes here to close current window????
myWindow.destroy()
class myWindow(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
display = [] #make sure display list is empty
# !!!! - this btnExit is in the class whose window I want to close
def btnExit(self):
choice = messagebox.askquestion("Exit Program?", "Are you sure?", icon='warning')
if choice == 'yes':
#???what goes here to close current window????
self.parent.deiconify()
self.top.destroy()
#now that we have a dictionary, create a readable formatted list and display it.
# use current filename NOTE: l1_text = StringVar() must be declared at top as global to be changed???
def displysList(self):
self.tk = Tk() #creates a NEW window each time
self.label1 = tk.Label(self, text = pathFilenameExt)
self.label1.pack(side=TOP, anchor=W, fill=X, expand=YES)
self.btn1 = tk.Button(self, text="New File", fg="blue", command=main)
self.btn1.pack(side=TOP, anchor=W, fill=X, expand=YES)
self.btn2 = tk.Button(self, text="EXIT", fg="red", command=btnExit)
self.btn2.pack(side=TOP, anchor=W, fill=X, expand=YES)
self.title('CURA json Lister')
self.resizable(width=True, height=True)
self.geometry(win_size)
#create scroll bar for listbox
self.scrollbary = tk.Scrollbar(self, orient='vertical')
self.scrollbary.pack(side = 'right', fill = 'y' )
self.scrollbarx = tk.Scrollbar(self, orient='horizontal')
self.scrollbarx.pack(side = 'bottom', fill = 'x' )
height =400 #in characters wider and taller than the screen to use all space available
width = 400
#list box create
self.list2Box=tk.Listbox(self, selectmode=EXTENDED)
self.list2Box.config(height=height, width=width)
self.scrollbary.config(command = self.list2Box.yview )
self.scrollbarx.config(command = self.list2Box.xview )
i = 0
while i < line_cnt:
self.list2Box.insert(END,display[i])
i += 1
self.list2Box.pack(side='left')
def main():
#no other setup needed here for now
windA = myWindow #CREATE AN INSTANCE TO CALL ppd
windA.displysList(root) #redisplay each new file
#end main
if __name__ == '__main__':
main()
root.mainloop()

Python GUI and function calling using Tkinter

I am new to python and trying to build a GUI that takes from date and to date from user and reads the data accordingly from a csv file and display the urine output (calculated in the csvimport fucntion). I also want to plot the graph for a specific time and urine output in that time.
Can anyone help me? My code so far is below and it isn't displaying any GUI. Please can anyone correct the basic errors and help me in running this?
import csv
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showwarning, showinfo
import datetime
#csv_file = csv.reader(open("C:\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.csv"))
from Tools.scripts.treesync import raw_input
class App(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.in_file = None
button1 = Button(self, text="Browse for a file", command=self.askfilename)
button2 = Button(self, text="Count the file", command=self.takedate())
button3 = Button(self, text="Exit", command=master.destroy)
button1.grid()
button2.grid()
button3.grid()
self.grid()
def askfilename(self):
in_file = askopenfilename()
if not in_file.endswith(('.csv')):
showwarning('Are you trying to annoy me?', 'How about giving me a CSV file, genius?')
else:
self.in_file=in_file
def CsvImport(csv_file):
dist = 0
for row in csv_file:
_dist = row[0]
try:
_dist = float(_dist)
except ValueError:
_dist = 0
dist += _dist
print ("Urine Volume is: %.2f" % (_dist*0.05))
def takedate(self):
from_raw = raw_input('\nEnter FROM Date (e.g. 2013-11-29) :')
from_date = datetime.date(*map(int, from_raw.split('/')))
print ('From date: = ' + str(from_date))
to_raw = raw_input('\nEnter TO Date (e.g. 2013-11-30) :')
to_date = datetime.date(*map(int, to_raw.split('/')))
in_file = ("H:\DataLog.csv")
in_file= csv.reader(open(in_file,"r"))
for line in in_file:
_dist = line[0]
try:
file_date = datetime.date(*map(int, line[1].split(' ')[1].split('/')))
if from_date <= file_date <= to_date:
self.CsvImport(in_file)
except IndexError:
pass
root = Tk()
root.title("Urine Measurement")
root.geometry("500x500")
app = App(root)
root.mainloop()
You are calling takedate method as soon as you are initializing your class. Removing parentheses(which means, call the method) would solve your issue.
button2 = Button(self, text="Count the file", command=self.takedate())
^^ remove these
Your GUI doesn't show up because takedate method makes your program to wait for user input because of raw_input(..) calls.
You should consider using Entry instead of raw_input() for getting user input.
EDIT: You can put two Entry in your __init__ then use Entry's get method in takedate. Roughly something like below.
def __init__(self, master):
...
...
self.userInputFromRaw = Entry(self)
self.userInputFromRaw.grid()
self.userInputToRaw = Entry(self)
self.userInputToRaw.grid()
def takedate(self):
...
from_raw = self.userInputFromRaw.get()
...
to_raw = self.userInputToRaw.get()
Also, you should add self parameter when defining your method since it is part of that class.
def CsvImport(self, csv_file):
If you not want to pass parameters into self.takedate() remove ()as below:
button2 = Button(self, text="Count the file", command=self.takedate)
or change to
button2 = Button(self, text="Count the file", command=lambda e=Null: self.takedate())
In this case you can pass e parameter to self.takedate(). Pass it to this maner:
command=lambda e=Null: self.takedate(e)
def takedate(self, parameter): pass

Tkinter click event does not trigger; keyboard does

I found this neat code here that uses Tkinter to display a series of images. I extended the code to use the 'z' and 'x' keys to browse through the images and 'q' to quit. Also, I would like to be able to click on the individual frames and obtain the image coordinates of where I clicked. While the keyboard interaction works just fine, the mouse click event does not get triggered. I wonder why that is, since the key strokes are triggered just fine.
This is the code I have:
#!/usr/bin/env python
from Tkinter import *
import Image, ImageTk
import os, sys
class Clicker:
def __init__(self, master, filelist):
self.top = master
self.files = filelist
self.index = 0
#display first image
filename = filelist[0]
if not os.path.exists(filename):
print "Unable to find %s" % filename
self.top.quit()
self.title = Label(text=os.path.basename(filename))
self.title.pack()
im = Image.open(filename)
self.tkimage = ImageTk.PhotoImage(im, palette=256)
self.lbl = Label(master, image=self.tkimage)
self.lbl.pack(side='top')
# the button frame
fr = Frame(master)
fr.pack(side='top', expand=1, fill='both')
back = Button(fr, text="back", command=lambda : self.nextframe(-1))
back.grid(row=0, column=0, sticky="w", padx=4, pady=4)
self.ilabel = Label(fr, text="image number: %d/%d" %
(self.index+1, len(self.files)))
self.ilabel.grid(row=0, column=1, sticky="e", pady=4)
self.evar = IntVar()
self.evar.set(1)
next = Button(fr, text="next", command=lambda : self.nextframe(1))
next.grid(row=0, column=3, sticky="e", padx=4, pady=4)
# events
fr.focus_set()
fr.bind("<Key>", self.key)
fr.bind("<Button 1>", self.left_click)
def left_click(self, event):
print (event.x,event.y)
def key(self, event):
if event.char == 'z':
# previous frame
self.nextframe(-1)
elif event.char == 'x':
# next frame
self.nextframe(1)
elif event.char == 'q':
# quit
self.top.quit()
def getImage(self, filename):
im = Image.open(filename)
return im
def nextframe(self,i=1, imgnum=-1):
if imgnum == -1:
self.index += i
else:
self.index = imgnum - 1
if self.index >= len(self.files):
self.index = 0
elif self.index < 0:
self.index = len(self.files) - 1
filename = self.files[self.index]
if not os.path.exists(filename):
print "Unable to find %s" % filename
self.top.quit()
self.title.configure(text=os.path.basename(filename))
self.evar.set(self.index+1)
self.ilabel.configure(text="image number: %d/%d" %
(self.index+1, len(self.files)))
im = self.getImage(filename)
self.tkimage.paste(im)
def getimgnum(self, event=None):
self.nextframe(imgnum=self.evar.get())
# --------------------------------------------------------------------
if __name__ == "__main__":
if not sys.argv[1:]:
print "Usage: click.py images*"
sys.exit()
filelist = sys.argv[1:]
root = Tk()
app = Clicker(root, filelist)
root.mainloop()
The code should work with any set of images, all of which have to have the same dimensions.
Edit: Interestingly, I can get the cursor position on a key stroke but not on a mouse click.
It seems like I found the answer myself:
If I replace the Frame with a Canvas, I am able to trigger a mouse click event. I am not sure why that is the case, but it works.

Categories