Why a file is empty after writing to it? - python

I got a tkinter app and a thread which writes some data to a file. If I let the thread finish it's job, the file is empty. If I terminate the program before the thread is done (clicking the red square in pyCharm) the file is filled with the data till the point of termination. Here is a code to reproduce the problem:
import tkinter as tk
import _thread
import numpy as np
img_list = []
def create_img_list():
for i in range(1000):
img = np.random.rand(385, 480)
img = img * 65535
img = np.uint16(img)
img_list.append(img)
def write_to_file():
f = open("test.Raw", "wb")
for img in img_list:
f.write(img)
f.close()
root = tk.Tk()
button = tk.Button(root, text="Click Me", command=_thread.start_new_thread(write_to_file, ())).pack()
create_img_list()
root.mainloop()
What is going on here and how do I fix it?

When I add print(img_list) to write_to_file() I see that this function is executed at start - without clicking button - even before create_img_list() runs (which creates list) so write_to_file() writes empty list.
You use command= incorrectly. It needs function name without () (so called "callback") but you run function and you assign its result to command=. Your code works like
result = _thread.start_new_thread(write_to_file, ()) # it executes function at start
button = tk.Button(root, text="Click Me", command=result).pack()
but you need
def run_thread_later():
_thread.start_new_thread(write_to_file, ())
button = tk.Button(root, text="Click Me", command=run_thread_later).pack()
Eventually you can uses lambda to create this function directly in command=
button = tk.Button(root, text="Click Me", command=lambda:_thread.start_new_thread(write_to_file, ())).pack()
BTW: you have common mistake
button = Button(...).pack()
which assign None to variable because pack()/grid()/place() return `None.
If you need access button later then you have to do it in two lines
button = Button(...)
button.pack()
If you don't need access button later then you can skip `button()
Button(...).pack()

Related

Tkinter: How to update a label text after a certain amount of time has passed?

So, I'm trying to create a basic Tkinter program which, when I press a button, updates the text on a label field, waits X amount of seconds and then update the label again.
For example:
I click the button, the label clears immediately after pressing it, then the program waits 3 seconds and shows "Hello" on screen.
The code shown below does not do what I want it to do because when I press the button, it remains pressed for X amount of time and then the text is updated inmediately. I want to press the button, clear the label, wait for 3 seconds and then show "Hello" on screen.
from tkinter import *
class Origin:
def __init__(self):
self.root = Tk()
self.root.geometry('800x600')
self.root.config(bg="black")
self.v = StringVar()
self.v.set('O R I G I N')
self.main_label = Label(self.root, textvariable=self.v, font="Arial 40", fg="white", bg="black")
self.main_label.place(x=240, y=150)
self.clear = Button(self.root, text='Clear', command=self.clear)
self.clear.place(x=400, y=400)
self.root.mainloop()
def clear(self):
#just to clear the string
self.v.set('')
self.root.after(3000, self.v.set('Hello'))
def main():
App = Origin()
if __name__ == '__main__':
main()
after needs callback - it means function's name without () and arguments. If you have to use function with argument then use `lambda
after(3000, lambda:self.v.set('Hello'))
or create function which doesn't need arguments
def callback():
self.v.set('Hello')
self.root.after(3000, callback)
Your current code works like
result = self.v.set('Hello')
self.root.after(3000, result)
It executes function self.v.set('Hello') at once and uses its result as callback in after().
EDIT: as #acw1668 said in comment you can also run function with arguments this way
self.root.after(3000, self.v.set, 'Hello')

Trying to print the output from my function inside my GUI window

Im trying to make a little program that endlessly prints out numbers inside GUI window, I can not find a way to print the out put of the function in a text box inside the GUI window instead of the python shell, please help, here is my code so far...
import sys
from tkinter import *
root = Tk()
def number(event):
x = 420
while True:
x +=420
print(x^70)
button_1 = Button(root, text="Start...")
button_1.bind("<Button-1>", number)
button_1.pack()
root.mainloop()
Thanks Harvey
You'll find it hard to constantly insert a value into a widget. The widget does not update with each insert. You can think of it has having a temporary variable for it. It can be accessed during the loop (as shown with print). However you'll notice that the widget itself doesn't update until the loop is over. So if you have while True then your widget will never update, and so you won't have the numbers streaming into the widget.
import sys
from tkinter import *
root = Tk()
def number():
x = 420
while x < 8400: # Limit for example
x +=420
textbox.insert(END, str(x^70)+'\n')
print(textbox.get(1.0, END)) # Print current contents
button_1 = Button(root, text="Start...", command=number) #Changed bind to command, bind is not really needed with a button
button_1.pack()
textbox = Text(root)
textbox.pack()
root.mainloop()

Tkinter text highlight with delay doesn't work

Goal: push a button to show and change the background of elements from a list one by one with a delay of n seconds until the entire text is highlighted.
I wrapped:
text2.insert(INSERT,sonsilabo,"silabo")
text2.pack()
time.sleep(2)
in a for-loop to do that for each element.
But the program still wait for the entire cycle to finish and show the final result.
from Tkinter import *
import Tkinter
import time
root = Tk()
text2 = Text(root)
text2.tag_config("silabo",background="green")
teksto = ['Sa','lu','to','n',' ','mi',' ','no','mi','ĝa','s',' ','A','b','de','l']
def kolorigu():
text2.delete('1.0', END)
for sonsilabo in teksto:
text2.insert(INSERT,sonsilabo,"silabo")
text2.pack()
time.sleep(2)
text2.pack()
B = Button(root, text ="Kolorigu", command = kolorigu)
B.pack()
root.mainloop()
Any idea?
After you add some text, you need to update the textbox by calling text2.update_idletasks:
def kolorigu():
text2.delete('1.0', END)
for sonsilabo in teksto:
text2.insert(INSERT,sonsilabo,"silabo")
########################
text2.update_idletasks()
########################
time.sleep(2)
Also, I removed the text2.pack() line inside kolorigu because it is unnecessary.

Simple gui that display a repeated set of images when a button is pushed

I made a very simple gui that has a button and shows an image(.gif). My goal is to output another .gif whenever you press the button. There are 2 .gif files in my file directory and the point is to keep switching between these two whenever you press the button.
#Using python2.7.2
import Tkinter
root = Tkinter.Tk()
try:
n
except:
n = 0
def showphoto(par):
if par%2 == 0:
try:
label2.destroy()
except:
pass
photo = Tkinter.PhotoImage(file="masc.gif")
label2 = Tkinter.Label(image=photo)
label2.image = photo
label2.pack()
else:
try:
label2.destroy()
except:
pass
photo = Tkinter.PhotoImage(file="123.gif")
label2 = Tkinter.Label(image=photo)
label2.image = photo
label2.pack()
myContainer1 = Tkinter.Frame(root, width = 100, height = 100)
myContainer1.pack()
def callback(event):
global n
showphoto(n)
n = n + 1
button1 = Tkinter.Button(myContainer1)
button1["text"]= "Next pic"
button1["background"] = "green"
button1.bind("<Button-1>", callback(n))
button1.pack()
root.mainloop()
The current code just outputs the first image (masc.gif) but when I press the button it doesn't switch to the other image(123.gif). What am I doing wrong?
This can achieved much easier with classes as the class holds all the data necessary without the use of global variables.
import Tkinter as tk
from collections import OrderedDict
class app(tk.Frame):
def __init__(self,master=None, **kwargs):
self.gifdict=OrderedDict()
for gif in ('masc.gif','123.gif'):
self.gifdict[gif]=tk.PhotoImage(file=gif)
tk.Frame.__init__(self,master,**kwargs)
self.label=tk.Label(self)
self.label.pack()
self.button=tk.Button(self,text="switch",command=self.switch)
self.button.pack()
self.switch()
def switch(self):
#Get first image in dict and add it to the end
img,photo=self.gifdict.popitem(last=False)
self.gifdict[img]=photo
#display the image we popped off the start of the dict.
self.label.config(image=photo)
if __name__ == "__main__":
A=tk.Tk()
B=app(master=A,width=100,height=100)
B.pack()
A.mainloop()
Of course, this could be done more generally ... (the list of images to cycle through could be passed in for example), and this will switch through all the images in self.gifs ...
This approach also removes the necessity to destroy and recreate a label each time, instead we just reuse the label we already have.
EDIT
Now I use an OrderedDict to store the files. (keys=filename,values=PhotoImages). Then we pop the first element out of the dictionary to plot. Of course, if you're using python2.6 or earlier, you can just keep a list in addition to the dictionary and use the list to get the keys.
button1 = Tkinter.Button(myContainer1)
button1["text"]= "Next pic"
button1["background"] = "green"
button1.bind("<Button-1>", callback(n))
First, you bind the <Button-1> event to None (that's what callback(n) evaluates to). You should bind it to callback (no parentheses a.k.a the call operator).
Second, I suggest you change callback to not accept any arguments, remove the bind call and create your button as:
button1 = Tkinter.Button(myContainer1, command=callback)

When you move Tkinter windows stops program

Hi
I made a music downloading program that works great it tells you the percent its done and then if i move the window at all it stops downloading .
I made a diffrent little script that downloads a specified mp3 on the web and i can move it as much as i want and it doesent stop.
the only reason im not putting up the code is that it is really long. Its around 1500 lines. Here is the small script that i made to download one file.
does any one know why it stops the program from working?
the little script:
from Tkinter import *
from urllib2 import *
admin = Tk()
Admin = Tk()
listbox = Listbox(admin, bg="PURPLE")
listbox.pack()
def __init__(self, master):
def replay():
Admin.destroy()
os.system('WhaleWire.exe')
frame = Frame(master)
frame.pack()
image1 = PhotoImage(file="whalewire.gif")
w = image1.width()
h = image1.height()
master.geometry("%dx%d+0+0" % (w, h))
# tk.Frame has no image argument
panel1 = Label(master, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
panel1.image = image1
self.e = Entry(frame)
self.e.grid(row=0, column=0)
b = Button(frame, text='Search', command=self.whale)
b.grid(row=0, column=1)
def library():
path = 'C:\WhaleWire\Downloaded'
aw=[]
for infile in glob.glob( os.path.join(path,'*.mp3') ):
libr = infile.split('Downloaded',1)
aw.append('\n')
aw.append(infile)
la = Label(Admin,width=100,height=50, text=aw).grid(row=0,column=7)
b2s = Button(Admin,text='Search', command=replay).grid(row=0,column=8)
b11 = Button(frame, text='Library', command=library)
b11.grid(row=0, column=3)
def fores():
chunks = 10000
dat = ''
song = '3 rounds and a sound'
url = 'http://bonton.sweetdarkness.net/music/Blind%20Pilot%20--%203%20Rounds%20and%20A%20Sound.mp3'
down = urlopen(url)
downso = 0
tota = down.info().getheader('Content-Length').strip()
tota = int(tota)
while 1:
a = down.read(chunks)
downso += len(a)
if not a:
break
dat += a
percent = float(downso) / tota
percent = round(percent*100, 1)
listbox.insert(END, percent)
listbox.update()
listbox.delete(0, END)
listbox.insert(END, percent)
listbox.update()
button = Button(Admin, text='Download', command=fores)
button.pack()
button = Button(Admin, text='Download', command=fores)
button.pack()
mainloop()
Most likely the problem is because you are calling update. You should never do that unless you know for certainty what the ramifications are. update causes a new event loop to be entered. Essentially, you end up with an infinite loop inside an infinite loop.
Try changing your update to update_idletasks and see if that solves your problem. This variation of update only processes "idle" events such as screen redraws and is considerably less likely to cause problems.
Also, you definitely don't need "update; insert; delete; update". That won't have any noticeable effect. A single call to update_idletasks after the delete is sufficient.
Finally, you can avoid the use of update_idletasks completely by rearranging your code. Write a function that reads a single chunk of data and updates the progress bar. Then, if it hasn't reached EOF, use after to call that function again a few milliseconds later. When it reaches EOF it stops calling itself. Doing this means you don't have to create your own potentially infinite loop, and the event loop is guaranteed to be entered once per iteration. Once this EOF is detected you can then call a function (again using after) to do any final processing.

Categories

Resources