I have very strange problem with gtk.Image(). Simple question; how to update image?
On window creation I create image and pack it… On that time I load image from disk. Now I start downloading image from url, and when it's done I just want to replace existing image with new one. I rewrite content of same file on disk and then do:
pixbuf = gtk.gdk.pixbuf_new_from_file(image_path)
self._user_avatar.set_from_pixbuf(pixbuf)
I have tried
self._user_avatar.set_from_file(image_path)
and
self._user_avatar.clear()
nothing works. When i restart app there is a new image and everything is ok.
gtk.Image.set_from_pixbuf is the right method, so your problem may come from something else. Try on the most simple piece of code to reproduce your problem.
Here's a working sample:
import pygtk
pygtk.require('2.0')
import gtk
pics = []
clicks = 0
def on_destroy (widget):
gtk.main_quit()
return False
def on_button_clicked (widget, image):
global clicks
clicks += 1
image.set_from_pixbuf (pics[clicks % len(pics)])
def create ():
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.connect("destroy", on_destroy)
pics.append (gtk.gdk.pixbuf_new_from_file("sample1.png"))
pics.append (gtk.gdk.pixbuf_new_from_file("sample2.png"))
image = gtk.Image()
image.set_from_pixbuf(pics[0])
button = gtk.Button ("Switch Image")
button.connect("clicked", on_button_clicked, image)
vbox = gtk.VBox()
vbox.pack_start (image)
vbox.pack_start (button)
window.add(vbox)
window.show_all()
if __name__ == "__main__":
create()
gtk.main()
Related
Database Loading Panel Screen StaticText is Blurred Out when Shown
Hello,
I have an odd issue with a loading screen. The problem is this: When I start with a main screen, then when you click on the 'next' button,
it goes to the database loading screen, when it is done, it prints on the screen that the Database Loading is complete.
The problem is that the loading screen text is blurred out for some reason. Also, something I discovered is that if you pop up a message dialog
box, the loading text is shown...
In this example, I am not actually loading into an actual database. Instead, I am just sleeping for seven seconds.
If you are confused as to what I am saying, just run the example, and you will see that the loading screen is all messed up...
Here is the code:
import wx
import time
class TextPanel(wx.Panel):
def __init__(self, parent, label):
self.__myParent = parent
wx.Panel.__init__(self, self.__myParent, size = (800, 800))
staticText = wx.StaticText(self, label = label)
class MainPanel(wx.Panel):
def __init__(self, parent):
self.__myParent = parent
wx.Panel.__init__(self, self.__myParent, size = (800, 800))
nextButton = wx.Button(self, label = 'Next')
nextButton.Bind(wx.EVT_BUTTON, self.__onNext)
def __onNext(self, event):
self.__myParent.onNextScreen()
class MainFrame(wx.Frame):
def __init__(self):
# Base contsructor.
wx.Frame.__init__(self, None, id = -1, title = 'Testing...', size = (800, 800))
self.__myMainPanel = MainPanel(self)
self.__myMainPanel.Show()
self.__myDatabase = TextPanel(self, 'Loading Data...')
self.__myDatabase.Hide()
self.__myFinalPanel = TextPanel(self, 'Database Loading Complete!')
self.__myFinalPanel.Hide()
def onNextScreen(self):
self.__myMainPanel.Hide()
self.__myDatabase.Show()
self.doDatabaseLoad()
self.__myDatabase.Hide()
self.__myFinalPanel.Show()
def doDatabaseLoad(self):
time.sleep(7) # before, this method would load data into a database...
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
frame.Show(True)
app.MainLoop()
print 'Exiting...'
Thanks all for your help,
I found an easy solution to this issue.
I first found out that wx.Panel.Update() gets called to repaint the screen when ever the event handler method returns. So, the loading panel seems to look like it needed to be repainted, as the static text box is all grayed out. I then just called self.__myDatabase.Update() after self.__myDatabase.Show().
Here is the new version of onNextScreen(self):
def onNextScreen(self):
self.__myMainPanel.Hide()
self.__myDatabase.Show()
self.__myDatabase.Update() # Fixes the bug...
self.doDatabaseLoad()
self.__myDatabase.Hide()
self.__myFinalPanel.Show()
Also, another way to fix it is to call SetLabel on the static text, but the above solution is better. It just seems that Update was not being called automatically, because it was in the middle of an event...
I am trying to create a user interface with a picture in its top right corner. Here is my code:
import tkinter as tk
import urllib.request
import base64 as b64
class my_ui(tk.Tk):
def __init__(self, parent):
tk.Tk.__init__(self,parent)
self.parent=parent
self.intialize()
def intialize(self):
self.grid()
#Welcome
label = tk.Label(self,text="Welcome to my UI", anchor='center',fg='white',bg='blue')
label.grid(column=0,row=0,columnspan=2,rowspan=2,sticky='EW')
#Buttons
button = tk.Button(self,text="Button 1",command=self.OnButtonClick)
button.grid(column=0,row=3,sticky='W')
def OnButtonClick(self):
print("You clicked the button!")
if __name__ == "__main__":
app = my_ui(None)
#Logo URL - just a smiley face
URL = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQCItlNQe0QaiuhkADUwgVTpx-Isaym6RAP06PHkzBe2Yza3a4rYIkHuB8"
u = urllib.request.urlopen(URL)
raw_data = u.read()
u.close()
b64_data = b64.encodestring(raw_data)
photo = tk.PhotoImage(data=b64_data)
logo = tk.Label(app, image=photo)
logo.image = photo # To save it in memory
logo.pack() # If I exclude this line, UI works fine.
app.title('My User Interface')
app.mainloop()
I am pulling a .gif from the web and returning a PhotoImage with my function. When I run this, I get no errors - rather, my tkinter window does not appear whatsoever. When I take out the line I mentioned in the comment, my UI comes up fine (buttons, but no image) with no errors.
I am unsure of what exactly the absence of the window means. I am running Python 3.4.1 on Mac OSx. Any help would be greatly appreciated!
When a tk.PhotoImage object gets garbage collected, the image is "released", so to speak. The image is still technically being used, so it's not destroyed, but it will get completely blanked out. Replace your return line with:
photo = tk.PhotoImage(data=b64_data)
return photo
Just be sure to declare photo as a global variable.
Currently I have a splash screen in place. However, it does not work as a real splash screen - as it halts the execution of the rest of the code (instead of allowing them to run in the background).
This is the current (reduced) arquitecture of my program, with the important bits displayed in full. How can I adapt the splash screen currently in place to actually allow the rest of the program to load in the background? Is it possible in python?
Thanks!
import ...
(many other imports)
def ...
def ...
(many other definitions)
class VFrams(wxFrame):
wx.Frame.__init__(self, parent, -1, _("Software"),
size=(1024, 768), style=wx.DEFAULT_FRAME_STYLE)
(a lot of code goes in here)
class MySplashScreen(wx.SplashScreen):
def __init__(self, parent=None):
aBitmap = wx.Image(name=VarFiles["img_splash"]).ConvertToBitmap()
splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT
splashDuration = 5000 # ms
wx.SplashScreen.__init__(self, aBitmap, splashStyle, splashDuration, parent)
self.Bind(wx.EVT_CLOSE, self.CloseSplash)
wx.Yield()
def CloseSplash(self, evt):
self.Hide()
global frame
frame = VFrame(parent=None)
app.SetTopWindow(frame)
frame.Show(True)
evt.Skip()
class MyApp(wx.App):
def OnInit(self):
MySplash = MySplashScreen()
MySplash.Show()
return True
if __name__ == '__main__':
DEBUG = viz.addText('DEBUG:', viz.SCREEN)
DEBUG.setPosition(0, 0)
DEBUG.fontSize(16)
DEBUG.color(viz.BLACK)
Start_Mainvars()
Start_Config()
Start_Translation()
Start_DB()
Start_Themes()
Start_Gui()
Start_Get_Isos()
Start_Bars()
Start_Menus()
Start_Event_Handlers()
app = MyApp()
app.MainLoop()
Thank you for all the help, this is how I changed the code (following the provided advice):
def show_splash():
# create, show and return the splash screen
global splash
bitmap = wx.Image(name=VarFiles["img_splash"]).ConvertToBitmap()
splash = wx.SplashScreen(bitmap, wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_NO_TIMEOUT, 0, None, -1)
splash.Show()
return splash
class MyApp(wx.App):
def OnInit(self):
global frame, splash
splash = show_splash()
Start_Config()
Start_Translation()
Start_DB()
Start_Themes()
Start_Gui()
Start_Get_Isos()
Start_Bars("GDP1POP1_20091224_gdp", "1 pork")
Start_Menus()
Start_Event_Handlers()
frame = VFrame(parent=None)
frame.Show(True)
splash.Destroy()
return True
if __name__ == '__main__':
DEBUG = viz.addText('DEBUG:', viz.SCREEN)
DEBUG.setPosition(0, 0)
DEBUG.fontSize(16)
DEBUG.color(viz.BLACK)
Start_Mainvars()
app = MyApp()
app.MainLoop()
Your code is pretty messy/complicated. There's no need to override wx.SplashScreen and no reason your splash screen close event should be creating the main application window. Here's how I do splash screens.
import wx
def show_splash():
# create, show and return the splash screen
bitmap = wx.Bitmap('images/splash.png')
splash = wx.SplashScreen(bitmap, wx.SPLASH_CENTRE_ON_SCREEN|wx.SPLASH_NO_TIMEOUT, 0, None, -1)
splash.Show()
return splash
def main():
app = wx.PySimpleApp()
splash = show_splash()
# do processing/initialization here and create main window
frame = MyFrame(...)
frame.Show()
splash.Destroy()
app.MainLoop()
if __name__ == '__main__':
main()
Just create the splash screen as soon as possible with no timeout. Continue loading and create your application's main window. Then destroy the splash screen so it goes away. Showing the splash screen doesn't stop other processing from happening.
You'll want to use two threads: one for the splash screen, one for whatever other code you want to execute. Both threads would run at the same time, providing the result you desire.
I have written this simple script in python:
import gtk
window = gtk.Window()
window.set_size_request(800, 700)
window.show()
gtk.main()
now I want to load in this window an image from web ( and not from my PC ) like this:
http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg
How can I do that ?
P.S. I don't want download the image ! I just want load the image from the URL.
This downloads the image from a url, but writes the data into a gtk.gdk.Pixbuf instead of to a file:
import pygtk
pygtk.require('2.0')
import gtk
import urllib2
class MainWin:
def destroy(self, widget, data=None):
print "destroy signal occurred"
gtk.main_quit()
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.image=gtk.Image()
response=urllib2.urlopen(
'http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg')
loader=gtk.gdk.PixbufLoader()
loader.write(response.read())
loader.close()
self.image.set_from_pixbuf(loader.get_pixbuf())
# This does the same thing, but by saving to a file
# fname='/tmp/planet_x.jpg'
# with open(fname,'w') as f:
# f.write(response.read())
# self.image.set_from_file(fname)
self.window.add(self.image)
self.image.show()
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
MainWin().main()
Download the image. Google on how to download files with python, there are easy-to-use libraries for that.
Load the image into a widget. Look up how to display an image in GTK.
Sorry for the lack of detail, but the answer would get long and you'd still be better off reading on those subjects somewhere else.
Hope it helps!
Here's a simple script using WebKit:
#!/usr/bin/env python
import gtk
import webkit
window = gtk.Window()
window.set_size_request(800, 700)
webview = webkit.WebView()
window.add(webview)
window.show_all()
webview.load_uri('http://www.dailygalaxy.com/photos/uncategorized/2007/05/05/planet_x.jpg')
gtk.main()
Take note, though, that this does in fact download the image.
In GTK, how can I scale an image? Right now I load images with PIL and scale them beforehand, but is there a way to do it with GTK?
Load the image from a file using gtk.gdk.Pixbuf for that:
import gtk
pixbuf = gtk.gdk.pixbuf_new_from_file('/path/to/the/image.png')
then scale it:
pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
Then, if you want use it in a gtk.Image, crate the widget and set the image from the pixbuf.
image = gtk.Image()
image.set_from_pixbuf(pixbuf)
Or maybe in a direct way:
image = gtk.image_new_from_pixbuf(pixbuf)
It might be more effective to simply scale them before loading. I especially think so since I use these functions to load in 96x96 thumbnails from sometimes very large JPEGs, still very fast.
gtk.gdk.pixbuf_new_from_file_at_scale(..)
gtk.gdk.pixbuf_new_from_file_at_size(..)
Scale image from URL. ( scale reference )
import pygtk
pygtk.require('2.0')
import gtk
import urllib2
class MainWin:
def destroy(self, widget, data=None):
print "destroy signal occurred"
gtk.main_quit()
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.image=gtk.Image()
self.response=urllib2.urlopen(
'http://192.168.1.11/video/1024x768.jpeg')
self.loader=gtk.gdk.PixbufLoader()
self.loader.set_size(200, 100)
#### works but throwing: glib.GError: Unrecognized image file format
self.loader.write(self.response.read())
self.loader.close()
self.image.set_from_pixbuf(self.loader.get_pixbuf())
self.window.add(self.image)
self.image.show()
self.window.show()
def main(self):
gtk.main()
if __name__ == "__main__":
MainWin().main()
*EDIT: (work out fix) *
try:
self.loader=gtk.gdk.PixbufLoader()
self.loader.set_size(200, 100)
# ignore tihs:
# glib.GError: Unrecognized image file format
self.loader.write(self.response.read())
self.loader.close()
self.image.set_from_pixbuf(self.loader.get_pixbuf())
except Exception, err:
print err
pass
Just FYI, here is a solution which scales the image based on window size (Implying you are implementing this in a class which extends GtkWindow).
let [width, height] = this.get_size(); // Get size of GtkWindow
this._image = new GtkImage();
let pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filePath,width,height,true);
this._image.set_from_pixbuf(pixbuf);
anyone doing this in C. This is how it's done
//Assuming you already loaded the file and saved the filename
//GTK_IMAGE(image) is the container used to display the image
GdkPixbuf *pb;
pb = gdk_pixbuf_new_from_file(file_name, NULL);
pb = gdk_pixbuf_scale_simple(pb,700,700,GDK_INTERP_BILINEAR);
gtk_image_set_from_pixbuf(GTK_IMAGE(image), pb);
actually when we use
gdk_pixbuf_scale_simple(pb,700,700,GDK_INTERP_BILINEAR); this function causes memory leakage (If we monitor task manager the memory requirement goes on increasing till it kills the process) when used with a timer event. How to solve that