I'm trying to get buttons with image and label and didn't succed. I can have button with label OR image but not both.
This is my part of code:
try:
pb = Pixbuf.new_from_file_at_size('myimg.jpg', 100, 100)
except:
pb = None
img = Gtk.Image()
img.set_from_pixbuf(pb)
button1 = Gtk.Button(xalign=0.5, yalign=1)
#button1.set_label(lbl)
button1.set_image(img)
button1.set_image_position(Gtk.PositionType.TOP)
button1.get_style_context().add_class("btn_article")
any idea ?
Thanks
Doc: Gtk.Button.set_image (widget, data): "The image will be displayed if the label text is NULL". The image here usually is an icon, therefore there is a icon-name associate with it and (depends on settings) if necessary showing the label according the icon. Reverse the situation, the label is used to find an icon associate with it and (if necessary) showing the icon.
Use:
button1.set_always_show_image (True)
or, if that doesn't succeed, create a container inside (in your case Gtk.Grid) and pack inside the button, i.e:
grid = Gtk.Grid ()
img = Gtk.Image()
img.set_from_pixbuf(pb)
label = Gtk.Label ('test')
grid.attach (img, 0, 0, 1, 1)
grid.attach (label, 0, 1, 1, 1)
grid.show_all ()
button1.add (grid)
Related
self.root.after(1000, self.label4.grid(row = 1, column = 0))
self.label3.grid(row = 0, column = 0)
self.label4 = Label(self.frame2, text = "")
self.saveButton = Button(self.frame2, text = "Save")
self.saveButton.grid(row = 20, column = 0)
I am using this line of code to display a Label after 1 second on a frame in tkinter. It has worked with .pack but not with .grid. I want a save button to appear underneath the label but the label to appear above it, hence why I am using the grid system. Here is what I'm getting:
Does anyone know how to fix this?
Thanks.
I've looked through several similar questions, but nothing is working. I have a Tkinter program that will open a second window when pressing a button. All of the images in the first window work great, but when I open the second window, I get the folling error message when I to to make a button with images:
_tkinter.TclError: image "pyimage5" doesn't exist
The images are defined and stored like this:
add_icon = PhotoImage(file = 'data/add_icon.gif'), subtract_icon = PhotoImage(file = 'data/subtract_icon.gif')
And they are called like this:
Button(edit, image = add_icon, command = add_new_pref, relief = FLAT) .grid(row = 2, column = 0, sticky = NSEW) Button(edit, image = subtract_icon, command = remove_pref, relief = FLAT) .grid(row = 3, column = 0, sticky = NSEW)
The images are defined after the creation of the new window. Any ideas?
Thanks to TheLizzard for their comment:
Change PhotoImage(file='data/add_icon.gif') to PhotoImage(file="data/add_icon.gif", master=edit)
I guess that the "MASTER" parameter will shift the image's main instance to the specified window.
I have a Popup for which I want to make its background translucent. I get the picture for bluring by rendering app.root to an Fbo, however when I restore the app.root widget from the Fbo, the widget is not being rendered every frame, but only when an Animation on any widget is running or the window is being dragged. The full file is here: https://github.com/XtremeWare/XtremeUpdater/blob/master/src/main.py#L493
snippet:
def render_background(*args):
fbo = Fbo(size=app.root.size, with_stencilbuffer=True)
with fbo:
Scale(1, -1, 1)
Translate(-app.root.x, -app.root.y - app.root.height, 0)
fbo.add(app.root.canvas)
fbo.draw()
tex = fbo.texture
fbo.remove(app.root.canvas)
tex.flip_vertical()
img = Image.frombytes('RGBA', tex.size, tex.pixels)
img = img.filter(ImageFilter.GaussianBlur(50))
tex = Texture.create(size=img.size)
tex.blit_buffer(
pbuffer=img.tobytes(), size=img.size, colorfmt='rgba')
tex.flip_vertical()
self.canvas.before.get_group('blur')[0].texture = tex
Clock.schedule_once(render_background)
I have figured it out. The following line needs to be at the end.
Window.canvas.insert(0, app.root.canvas)
I am creating a GUI with a ".grid" of buttons. And I want to make each of those buttons display a different image on press. So when I click button 1, it will bring up "image1", on the bottom of the buttons. When I click button 2, it will bring up "image2" and etc.
Through some research, I was able to make the buttons run a function that takes in a parameter through the method below. However, I can't seem to make the button display the image. rather, it just makes an empty white space underneath the buttons, when I press any buttons.
Disclaimer:
- I do not want there to be loads of images, there will only be 1 image, and it will change depending on what button i press.
Here's the code:
from tkinter import *
def funct(numimg):
image = PhotoImage(file="image"+str(numimg)+".png")
label = Label(image=image)
label.grid(row = row_no+1, column = 0, columnspan = num_of_cols)
def make_funct(number):
return (lambda: funct(number))
root= Tk()
row_no = -1
buttons = []
num_of_cols = 3
root.resizable(0, 0)
numfiles = 6
for x in range(0, numfiles):
if(x % num_of_cols is 0):
row_no+=1
buttons.append(Button(root, text = "Button "+str(x), bg = '#4098D3', width = 30,height = 13,command = make_funct(x)))
buttons[x].grid(row = row_no, column = x % num_of_cols)
root.mainloop()
So my question is, how do you make each individual button, display a different image when it is pressed? this program right here just leaves an empty white space in replace of the image, and the image is not shown.
There are two major problems with the code you posted.
The first is basically the same as in this question: Why does Tkinter image not show up if created in a function?. You should keep a reference to the PhotoImage object, else it will be garbage collected and it will not show.
The second is that you create a new Label on every button click. You should only make one Label and change the image with the label.config() method.
I would (without wrapping your GUI in a class, which might be a nicer solution) load all images on initialization, save them in a list as attribute of the label and only change the image upon a button click.
I also removed your make_funct function and replaced it with a lambda, which is the most used way to pass variables to a function on callback.
from tkinter import *
def funct(numimg):
label.config(image=label.images[numimg])
root= Tk()
row_no = -1
buttons = []
num_of_cols = 3
root.resizable(0, 0)
numfiles = 3
for x in range(0, numfiles):
if(x % num_of_cols is 0):
row_no+=1
buttons.append(Button(root, text = "Button "+str(x), bg = '#4098D3', width = 30,height = 13, command = lambda n=x: funct(n)))
buttons[x].grid(row = row_no, column = x % num_of_cols)
label = Label(root)
label.grid(row = row_no+1, column = 0, columnspan = num_of_cols)
label.images=[]
for x in range(0, numfiles):
label.images.append(PhotoImage(file="image"+str(x)+".png"))
root.mainloop()
I'm writing a PyGtk paint program based on the basic tutorial found here.
Is there any way to add an image to the drawing area so that you can still draw over the image? Like a stamp, or an imported photo for example. I've tried adding a gtk.Image() but gtk.DrawingArea object has no attribute add.
self.window = gtk.Window((gtk.WINDOW_TOPLEVEL))
self.window.set_title ("Canvas")
self.window.set_position(gtk.WIN_POS_CENTER)
hbox = gtk.HBox(False, 0)
self.window.add(hbox)
self.window.set_resizable(False)
# Create the drawing area
drawing_area = gtk.DrawingArea()
drawing_area.set_size_request(screenWidth-350, screenHeight-100)
hbox.pack_start(drawing_area, True, True, 0)
drawing_area.show()
You have to draw the image (as a gtk.gdk.Pixbuf, not gtk.Image) onto the backing pixmap yourself using gtk.gdk.Drawable.draw_pixbuf(). Only container widgets (widgets that can contain other widgets) have an add() method, and gtk.DrawingArea is not one.
pixbuf = gtk.gdk.pixbuf_new_from_file(image_filename) #one way to load a pixbuf
pixmap.draw_pixbuf(None, pixbuf, 0, 0, x, y, -1, -1, gtk.gdk.RGB_DITHER_NONE, 0, 0)