I have googled about this problem but all i could find is how to add an image to a button. This is the opposite of my question kinda.
I have tried to use the image as a frame but did not work.
I have an image in mainwindow and i would like to add a button under the image. When i apply this the image disappears and i see only a little button somewhere on the screen. I add the button in the header method and below i share the picture with button and without button. Please ignore ugliness of the images.
class MainMenu():
def __init__(self,root,UserName):
self.root = root
self.root.geometry("800x500")
self.root.configure(background = "grey")
self.Body()
self.Header(UserName)
self.root.mainloop()
def Body(self):
MidImg = ImageTk.PhotoImage(Image.open("MyTkinter/Animasyon Çember/don1.png").resize((450,300)))
#MidImg = ImageTk.PhotoImage(Image.open("MyTkinter/images/aot1.jpg").resize((450,300)))
labelImg = Label(self.root,image = MidImg)
labelImg.image = MidImg
#labelImg.pack(side = "top",fill = "both", expand = "yes")
labelImg.grid(columnspan = 2,sticky = "N",padx = 180)
but= Button(labelImg, text = "Para Ekle")
but.grid(sticky = "SE")
Without button
With button
Related
I have a label with an image, and a button which should update the label / delete the image in the label, so I can put a new image into the same label via label.config.
I tryed to use something like that: whenever you click on the button the it should remove the image with label.config(image = None) but it doesnt work, if I load new images into the label the old ones are still there:
# here is the label initialized
global brand_preview
brand_preview = Label(root, image = None)
brand_preview.place(x = 10, y = 60)
# thats the button which have to clear the label image
self.top_brand = Button(root, text = "clear", bg = "snow3", command=clear_label_image)
self.top_brand.place(x = 550, y = 60)
# thats how I load a photoimage into the label
photoimg_brand = ImageTk.PhotoImage(im_thumb)
brand_preview.image = photoimg_brand
brand_preview.config(image = photoimg_brand)
# Thats how the button works
def clear_label_image():
brand_preview.config(image = None)
brand_preview.image = None
All I want now that if we I click the Button the brand_preview loses the image / the image gets deleted
EDIT:
The main issue is solved, but that only works if the button only has to delete the image. If I want to delete and add a new one it doesnt work
def clear_label_image():
brand_preview.config(image = "")
photoimg_brand = ImageTk.PhotoImage(im_thumb)
brand_preview.image = photoimg_brand
brand_preview.config(image = photoimg_brand)
You're very close - the image parameter just needs an empty string rather than None.
def clear_label_image():
brand_preview.config(image='')
After some Googling, I found this solution
def clear_label_image():
#brand_preview.config(image = None)
brand_preview.image.blank()
brand_preview.image = None
This definitely clears the image from the button. I haven't tried changing to a new image.
I just copied it from the Web, and it worked. I created the image with
photoimg_brand = tk.PhotoImage(file='/usr/share/httpd/icons/world1.gif')
I did this with python 2.7, and my only import was import Tkinter as tk
If you are using label to show the image then you can do this:
label.pack_forget()
label should be global
The following project is supposed to show a message when clicking a certain colored button. But, whenever I execute the program it shows blank(white) buttons in the correct alignment, etc. Due to some reason the images are not loaded.
In future, I plan to add different images hence testing with colored image created in Paint and not in-built commands to show the color.
I will add the result below after the code.
Edit: All images are 100x100 pixels created in Microsoft Paint.I have tried other modules like PIL but to no avail.
# importing the module
import tkinter
import tkinter.messagebox
from tkinter import *
# importing the module
# initialising tkinter
class window(Frame):
def __init__(self,master = None):
Frame.__init__(self,master)
self.master = master
# initialising tkinter
# creating the window
root = Tk()
app = window(root)
root.geometry("350x350")
# creating the window
# colours
WHITE = (255,255,255)
BLACK = (0,0,0)
BLUE = (0,0,255)
RED = (255,0,0)
# colours
# image
red_image = "red.png"
blue_image = "blue.png"
yellow_image = "yellow.png"
green_image = "green.png"
# image
# creating a button function
def create_button(x,y,color,color2,picture):
click = Button(root, image = PhotoImage(picture), width= 150, height=150, command = lambda : tkinter.messagebox.showinfo( "Hello Python", "This is " + color))
click.image = PhotoImage(picture)
click.grid( row = x, column = y)
# creating a button function
create_button(0,0,'red','pink',red_image)
create_button(0,2,'blue','lightblue',blue_image)
create_button(2,0,'green','lightgreen',green_image)
create_button(2,2,'yellow','lightyellow',yellow_image)
# starting the widget
root.mainloop()
# starting the widget
There are two issues in your code:
You passed filename to PhotoImage() without using file keyword: PhotoImage(picture) should be PhotoImage(file=picture)
You did not keep the reference of the image assigned to button, but another instance of image
Below is the updated create_button() function that fixes the issues:
def create_button(x, y, color, color2, picture):
image = PhotoImage(file=picture)
click = Button(root, image=image, width=150, height=150, command=lambda: tkinter.messagebox.showinfo("Hello Python", "This is "+color))
click.image = image
click.grid(row=x, column=y)
For adding image in Button you have not use appropriate keywords.
Here is a simple example for you to add image in button
from tkinter import *
from tkinter.ttk import *
# creating tkinter window
root = Tk()
# Adding widgets to the root window
Label(root, text = 'Image adding', font =( 'Verdana',15)).pack(side = TOP, pady = 10)
# Creating a photoimage object to use image
photo = PhotoImage(file = "C:\Gfg\circle.png")
# here, image option is used to
# set image on button
Button(root, text = 'Click Me !', image = photo).pack(side = TOP)
root.mainloop()
I think it may help you
I'm trying to make a page for my tkinter GUI and when I try to put a button to go to the previous page at the bottom left corner it never seems to work.
I have created a frame to put inside the window, packing it to the left side, then packing the button in that frame to the bottom of it. I have also tried it the other way round, so frame at the bottom and the button on the left. Changing the values of the size of the frame seems to do nothing.
previousPageF = tk.Frame(self, height = "100", width = "500")
previousPageF.pack(side = "bottom")
previousPageB = tk.Button(previousPageF, font = BASIC_FONT, text = "<--", command = lambda: controller.show_frame(LoginPage))
previousPageB.pack(side = "left")
Picture of the results of the code
I want it to be the very bottom left corner and not in the position it is currently in. I would like to use pack as it is not a very complicated GUI design and pack also puts it automatically in the centre of the window, whereas with grid I would have to do some maths to get it perfectly in the centre. Furthermore, I have done all of my other work with pack so I would prefer not to change all of it just to get this to work.
Figured it out with trial and error.
previousPageB = tk.Button(self, font = BASIC_FONT, text = "<--", command = lambda: controller.show_frame(LoginPage))
previousPageB.pack(anchor = "w", side = "bottom")
This will result in the button being in the bottom left corner:
enter image description here
It will also work if you do:
previousPageB = tk.Button(self, font = BASIC_FONT, text = "<--", command = lambda: controller.show_frame(LoginPage))
previousPageB.pack(anchor = "s", side = "left")
You don't have to use frame for this.You can directly mention fill='none' and side='left' in pack to place widget to left in window.
import tkinter as tk
root = tk.Tk()
previousPageB = tk.Button(root, text = "<--", command = lambda:
controller.show_frame(LoginPage))
previousPageB.pack(fill='none',side = "left")
I'm new to Python, I'm trying to add widgets in an window which can be used when we click on an button when in Tkinter GUI.
I'm unable to add an window into the GUI button and I'm doubtful about the Code which can be Implemented as well. I hope I could get some inputs on this.
I'm running on IDLE 3.6.3.I would be grateful if someone could point out the additions that could be made and the changes in the current code.
ConnectLogo=PhotoImage(file="Connect.png")
Connect = Button(win,image=ConnectLogo,text = "Connect", font = myFont,height =100 , width = 100,compound=TOP,bg = "orange")
Connect.grid(row=3,column=1,padx=50,pady=40)
FrequencyLogo=PhotoImage(file="Frequency.png")
Frequency = Button(win,image=FrequencyLogo, text = "Frequency", font = myFont, height = 100, width =180,compound=TOP,bg = "Yellow")
Frequency.grid(row=3,column=2,padx=10)
MaskLogo=PhotoImage(file="Mask.gif")
Mask = Button(win,image=MaskLogo, text = "Mask", font = myFont, height = 100, width =180,compound=TOP,bg = "yellow")
Mask.grid(row=6,column=2,padx=10)
You can make a function, which will implement TopLevel.
This creates a new window into which you can add widgets, add them inside the function.Inside the function you root becomes window
from tkinter import *
root = Tk()
def new_window():
window = TopLevel(root)
...widgets like label, entry etc
label = Label(window,....)
btn = Button(...., command = new_window)
btn.pack()...(anything)
I'm newbie in this site, but I have a question.
This is my code in Tkinter. My problem is that when I go to put an image inside a button, it does not display it.
Only in this program, because if I write the same things in a new clean file, it works. What's the problem ?
def newWindow():
root.destroy() #this destroy the main window
window = Tk()
window.title("Provisory title")
window.resizable(False,False)
window.geometry("420x430")
#BUTTON
bt3 = Button(window)
#BUTTON POSITIONING
bt3.place(x = 10, y = 10)
#image button
calc = PhotoImage(file= "icon_Calculator.png")
bt3.config(image = calc, width="100",height="100")