I have the following script based on a solution of another Stackoverflow question. I have a tkinter GUI with a picture that should refresh when there is taken a new one.
class App(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def callback(self):
self.root.quit()
def run(self):
self.root = Tk()
self.root.geometry("{0}x{1}+0+0".format(800,800))
files = sorted(os.listdir(os.getcwd()),key=os.path.getmtime)
Label(self.root,image = ImageTk.PhotoImage(Image.open(files[-1]).resize((300, 200)))).grid(row = 1, column = 0)
Label(self.root, text=files[-1]).grid(row=0, column=0)
self.root.mainloop()
def update(self):
files = sorted(os.listdir(os.getcwd()),key=os.path.getmtime)
img1 = self.ImageTk.PhotoImage(self.Image.open(files[-1]).resize((300, 200)))
Label(self.root,image = img1).grid(row = 1, column = 0)
Label(self.root, text=files[-1]).grid(row=0, column=0)
self.root.update()
app = App()
app.update()
I get this error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 176, in paste
tk.call("PyImagingPhoto", self.__photo, block.id)
_tkinter.TclError: invalid command name "PyImagingPhoto"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./mainloop.py", line 98, in <module>
app.update()
File "./mainloop.py", line 79, in update
img1 = ImageTk.PhotoImage(Image.open(files[-1]).resize((300, 200)))
File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 115, in __init__
self.paste(image)
File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 182, in paste
_imagingtk.tkinit(tk.interpaddr(), 1)
OverflowError: Python int too large to convert to C ssize_t
What am i doing wrong? Creating a label with with the file name does work. The filename is a valid file. The script does work when it is not inside the thread.Threading part. I would like to update the screen with the newest picture when a Raspberry GPIO button is pressed (script will take a new photo with a camara). So the screen should load with the lastest taken picture.
tkinter is not thread-safe (by default). All operations dealing with the GUI must be done in the main thread. You can do what you are asking using callbacks on events, but threading can and will break tkinter unless you implement an event queue that feeds into the mainloop.
Additionally, the stack trace seems to point to not your code here- that stack trace doesn't intersect with the presented code at all.
Also, calling root.update() is generally a bad idea- it starts another local event loop, which may be able to call another root.update() for another event loop ad infinium. root.update_idletasks() is much safer than a full root.update()
Related
I am making this app using tkinter and urllib which is supposed to be a download manager and decided to use multiprocessing for each download, however, I ran into a problem. Each time that I hit the 'Download' button to get the information from my widgets to start the download, I got a Name Error saying that my widgets are not defined. I tried making the widgets global and even called the widgets globally in my function and even passing them as arguments but apparently none of them worked. I thought that it may have to do with the structure of my program so I tested it on a mock program which was much simpler and still got the same error. Why is this happening? The mock program:
from tkinter import *
from multiprocessing import Process
times_clicked = 0
def change_lbl():
global times_clicked
times_clicked += 1
lbl.config(text=f"Clicked: {times_clicked}")
if __name__ == '__main__':
root = Tk()
root.geometry("300x300")
lbl = Label(root, text="Waiting for clicks...")
def start():
Process(target=change_lbl, daemon=True).start()
btn = Button(root, text="Add to clicks...", command=start)
lbl.pack(pady=10)
btn.pack()
root.mainloop()
The error:
Process Process-1:
Traceback (most recent call last):
File "C:\Users\Family\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "C:\Users\Family\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Family\PycharmProjects\8-bit Downloader\pyfile.py", line 10, in change_lbl
lbl.config(text=f"Clicked: {times_clicked}")
NameError: name 'lbl' is not defined
You have to define the lbl write something like
lbl = #something you need
I am fairly new to python and very new to GUI's so please let me know if I have made an obvious mistake.
This is also my first post so please let me know if I need to edit my question in some way.
I don't understand why I am getting errors when trying to set an icon to a tkinter GUI.
I have already tried the following posts:
tkinter TclError: error reading bitmap file
https://www.delftstack.com/howto/python-tkinter/how-to-set-window-icon-in-tkinter/
There are other stack exhange posts on this topic but they are for windows and/or their OP's normally can get the icon to apear once then it doesn't work. This isn't my situation the programs fail to run. Though I have tried some of their suggestions as well.
I have tried three different ways of coding this:
Method 1:
from tkinter import *
import os
class MainWindow:
def __init__(self, master):
self.master = master
master.title('TD')
#self.iconbitmap(default = 'fist.ico')
root = Tk()
root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))
my_gui = MainWindow(root)
root.mainloop()
Result:
runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):
File "~./envs/thermTD_project/thermTD/untitled2.py", line 23, in <module>
root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))
File "/usr/lib/python3.8/tkinter/__init__.py", line 2116, in wm_iconphoto
self.tk.call('wm', 'iconphoto', self._w, "-default", *args)
TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.png" as iconphoto: not a photo image
File existence check:
In [42]: os.path.exists(os.path.join(os.getcwd(),'favicon-3.png'))
Out[42]: True
Method 2:
from tkinter import *
import os
class MainWindow:
def __init__(self, master):
self.master = master
master.title('TD')
#self.iconbitmap(default = 'fist.ico')
root = Tk()
root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))
my_gui = MainWindow(root)
root.mainloop()
Result:
In [43]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):
File "~./envs/thermTD_project/thermTD/untitled2.py", line 22, in <module>
root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))
File "/usr/lib/python3.8/tkinter/__init__.py", line 2071, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
TclError: bitmap "~./envs/thermTD_project/thermTD/favicon-3.ico" not defined
File existence check:
In [44]: os.path.exists(os.path.join(os.getcwd(),'favicon-3.ico'))
Out[44]: True
Method 3:
from tkinter import *
import os
class MainWindow:
def __init__(self, master):
self.master = master
master.title('TD')
#self.iconbitmap(default = 'fist.ico')
root = Tk()
img = PhotoImage(os.path.join(os.getcwd(),'favicon-3.ico'))
root.tk.call('wm','iconphoto', root._w, img)
my_gui = MainWindow(root)
root.mainloop()
Result:
In [45]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):
File "~./envs/thermTD_project/thermTD/untitled2.py", line 21, in <module>
root.tk.call('wm','iconphoto', root._w, img)
TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.ico" as iconphoto: not a photo image
File existence check:
See method 1
The code without any of the icon setting lines works as expected.
I have tried to gif, png and ico on each method. I am trying other file types now.
NB: While the filename is favicon actually they are ico, gif, png files of 64x64 pixles. Even the favicon ico only has 1 type of icon in it (if thats the right phrase). Its just I got them from a favicon which I converted to different formats using mogrify.
OS: Ubuntu 20.04.1 LTS
python 3.8.2
IDE: spyder 4.1.5
P.S. I would be gratefull to know what the syntax for including this in my init function. I am trying to do object orientated GUI's I am very much still learning.
I am using wxPython on Python 2.7.
I would like some help with creating a button with bitmap images.
I am using this video https://www.youtube.com/watch?v=Y7f0a7xbWHI,
and I followed the codes and typed
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Frame aka window',size=(300,200))
panel=wx.Panel(self)
pic=wx.Image("back.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
self.button.SetDefault()
def doMe(self, event):
self.Destroy
to create a button with an image. I got an error stating Invalid Image.
I saved the bitmap image in a folder that has .py file I am working with.
I feel like I am saving the image in the wrong place?
Thank you in advance.
The error I received
Traceback (most recent call last):
File "C:\Python27\FrameWindow.py", line 81, in <module>
frame=bucky(parent=None,id=-1)
File "C:\Python27\FrameWindow.py", line 17, in __init__
pic=wx.Image("back.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
File "C:\Python27\lib\site-packages\wx\core.py", line 708, in _Image_ConvertToBitmap
bmp = wx.Bitmap(self, depth)
wxAssertionError: C++ assertion "image.IsOk()" failed at ..\..\src \msw\bitmap.cpp(922) in wxBitmap::CreateFromImage(): invalid image
I got it to work by adding
"locale = wx.Locale(wx.LANGUAGE_ENGLISH)"
under
class MainFrame(wx.Frame):
def __init__(self):
now I do not get the error message, and it runs as it should.
The error code I received for this problem was:
Traceback (most recent call last):
File "C:\Python27\panel test.py", line 21, in <module>
frame = MainFrame()
File "C:\Python27\panel test.py", line 11, in __init__
pic = wx.Bitmap("back.bmp", wx.BITMAP_TYPE_ANY)
wxAssertionError: C++ assertion "strcmp(setlocale(LC_ALL, NULL), "C") == 0" failed at ..\..\src\common\intl.cpp(1579) in wxLocale::GetInfo(): You probably called setlocale() directly instead of using wxLocale and now there is a mismatch between C/C++ and Windows locale.
Things are going to break, please only change locale by creating wxLocale objects to avoid this!
The error given by my primary question was solved by the codes given by Rolf by Saxony.
Use wx.BITMAP_TYPE_ANY rather than wx.BITMAP_TYPE_BMP it takes the guessing out of whether it really is a BMP or not
or use wx.Bitmap() directly.
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Frame aka window',size=(300,200))
panel=wx.Panel(self)
#
# Use wx.BITMAP_TYPE_ANY rather than wx.BITMAP_TYPE_BMP
#
#pic=wx.Image("Discord.bmp", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#
# or use wx.Bitmap()
pic = wx.Bitmap("Discord.bmp", wx.BITMAP_TYPE_ANY)
#
self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
self.Show()
def doMe(self, event):
self.Destroy()
if __name__ == "__main__":
app = wx.App()
frame = MainFrame()
app.MainLoop()
In principle you should name your image/s with a full pathname. If you have many images, then have an image directory and join that directory name to your image names as you use them, which again gives you a full path (/home/images/back_button.bmp)
I am trying to simultaneously read data from a HID with pywinusb and then update a tkinter window with that data. When something happens on the HID side, I want my tkinter window to immediately reflect that change.
Here is the code:
import pywinusb.hid as hid
from tkinter import *
class MyApp(Frame):
def __init__(self, master):
super(MyApp, self).__init__(master)
self.grid()
self.setupWidgets()
self.receive_data()
def setupWidgets(self):
self.data1 = StringVar()
self.data1_Var = Label(self, textvariable = self.data1)
self.data1_Var.grid(row = 0, column = 0, sticky = W)
def update_data1(self, data):
self.data1.set(data)
self.data1_Var.after(200, self.update_data1)
def update_all_data(self, data):
self.update_data1(data[1])
#self.update_data2(data[2]), all points updated here...
def receive_data(self):
self.all_hids = hid.find_all_hid_devices()
self.device = self.all_hids[0]
self.device.open()
#sets update_all_data as data handler
self.device.set_raw_data_handler(self.update_all_data)
root = Tk()
root.title("Application")
root.geometry("600x250")
window = MyApp(root)
window.mainloop()
When I run the code and make the device send data, I get this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python 3.3\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Program Files\Python 3.3\lib\tkinter\__init__.py", line 501, in callit
func(*args)
TypeError: update_data1() missing 1 required positional argument: 'data'
I guess my question is:
How do I continually update the label with the current data from the HID?
How can I pass the new data to update_data1()?
Edit: Should I be using threading, so that I have one thread receiving data and the mainloop() thread periodically checking for new data? I haven't used threading before, but could this be a solution?
If there is a better way to do this, please let my know.
Thanks!
self.data1_Var.after(200, self.update_data1) is the problem. You need to pass self.update_data1's parameter to self.data1_Var.after (e.g. self.data1_Var.after(200, self.update_data1, some_data)). Otherwise after 200 milliseconds, self.update_data1 will be called without the parameter, causing the error you are seeing.
BTW, why don't directly edit the label's text instead of putting the code in self.update_all_data. It's not clear to me why self.data1_Var.after(200, self.update_data1) is required, because whenever new data is received, update_all_data is called, which calls update_data1 which updates the text.
I'm looking at the example codes online, seems like I'm doing exactly like them. But the event seems to load as soon as the ui loads. What am I doing wrong?
From the code below, the click function doesn't load right when ui is loaded. But when I click the button, it throws:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
TypeError: clicky() takes no arguments (1 given)
class LogIn:
def __init__(self):
self.root = Tk();
self.root.title("480 - Chat Project Login");
self.root.geometry("275x125");
self.username = Label(self.root, text="Username: ");
self.username.pack(side=LEFT);
self.username.place(x=40, y=20);
self.u_entry = Entry(self.root, width=20);
self.u_entry.pack(side=RIGHT, ipady=4, ipadx=4);
self.u_entry.place(x=110, y=20);
self.password= Label(self.root, text="Password: ");
self.password.pack(side=LEFT);
self.password.place(x=40, y=50);
self.p_entry = Entry(self.root, width=20);
self.p_entry.pack(side=RIGHT, ipady=4, ipadx=4);
self.p_entry.place(x=110, y=50);
self.button = Button(text="Send", width=8);
self.button.pack(side=RIGHT, ipady=4, ipadx=4);
self.button.place(x=168, y=80);
self.button.bind("<Button-1>", clicky);
self.root.mainloop();
def clicky():
print "hello";
if __name__ == "__main__":
LogIn();
# Client();
You need self.button = Button(text="Send",width=8,command=clicky).
There's a difference between callbacks registered via command and callbacks registered via bind. With command, the callback doesn't get passed any additional arguments. With bind, the callback gets passed an event object.
Also, in case it wasn't clear, note that command is specific to Button objects whereas bind can be applied to any Tkinter widget.