Python Forum
Tkinter button images not showing up
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter button images not showing up
#1
I have the following code



from tkinter import *
from tkinter import messagebox

def read_account_balance():
    try:
        with open("money.txt","r") as file:
            money=float(file.read())
    except FileNotFoundError:
        with open("money.txt","w")as file:
            money=0.00
            file.write(money)
    except ValueError:
        with open("money.txt","w")as file:
            money=0.00
            file.write(money)
    return money
window=Tk()
def deposit_money():
    pass

def withdraw_money():
    pass

def money_transfer():
    pass

def display_account_balance():
    budget=read_account_balance()
    messagebox.showinfo(title="Balance Information",message=f"Account Balance is {budget}0")
read_account_balance()
window.config(padx=50,pady=50)
window.title("Bank App")
canvas=Canvas(width=400,height=275)
bank_pic=PhotoImage(file="./bank.png")
picture=canvas.create_image(200,149,image=bank_pic)
canvas.grid(row=1,column=0)
withdraw_img=PhotoImage("./withdraw.jpg")
deposit_img=PhotoImage("./deposit.jpg")
money_transfer_pic=PhotoImage("./money_transfer_pic.png")
deposit_button=Button(text="Deposit Money",image=deposit_img,command=deposit_money)
deposit_button.grid(row=1,column=1)
deposit_button.img_reference = deposit_img
withdraw_button=Button(text="Withdraw Money",image=withdraw_img,command=withdraw_money)
withdraw_button.grid(row=2,column=1)
withdraw_button.img_reference = withdraw_img

money_transfer_button=Button(text="Money Transfer",image=money_transfer_pic,command=money_transfer)
money_transfer_button.grid(row=1,column=2)
money_transfer_button.img_reference = money_transfer_pic

account_balanace_image=PhotoImage(file="./account-balance.png")
account_balanace_button=Button(text="Account Balance",image=account_balanace_image,command=display_account_balance)
account_balanace_button.img_reference=account_balanace_image
window.mainloop()
The problem is that the buttons (with their images do not appear in the window). If i change the canvas dimensions i can almost see some of the buttons to the left of the image but their images still dont appear. Unless i have confused rows with columns the grid assignments would have the buttons be placed under the picture with their respective images. Please help
Reply
#2
I am not going to help you until you fix your post by adding the correct python tags. You've been warned about this on several occasions.
Reply
#3
(Aug-31-2023, 05:22 PM)deanhystad Wrote: I am not going to help you until you fix your post by adding the correct python tags. You've been warned about this on several occasions.

Thanks. Sorry i tried the insert code snippet but it wouldnt work. Hope my edit is now correct according to the rules.
Reply
#4
Much better. Thankyou.

I don't understand what you are trying to do. You make a canvas and add that to the window. Then you make some buttons, but you are not telling any of the buttons where they are supposed to appear. By default they appear in the root window, but that is already filled with canvas. What is the desired result?
Reply
#5
(Aug-31-2023, 05:33 PM)deanhystad Wrote: Much better. Thankyou.

I don't understand what you are trying to do. You make a canvas and add that to the window. Then you make some buttons, but you are not telling any of the buttons where they are supposed to appear. By default they appear in the root window, but that is already filled with canvas. What is the desired result?

I am trying to add the pictures to the button widgets and have the buttons appear underneath the main picture of the bank. I thought the .grid function does that with the row and column args. The functionality is nonexistent for the time being i just wanted to make the GUI first
Reply
#6
When you make the buttons you need to tell them their parent is "window". For example:
deposit_button=Button(window, text="Deposit Money" image=deposit_img, command=deposit_money)
But that is not your problem. As I said, the default is put the widgets in the root window.

The problem is this:
bank_pic=PhotoImage(file="./bank.png")  # Works
withdraw_img=PhotoImage("./withdraw.jpg")  # Does not work
See the difference? No, it is not the image type.
Reply
#7
(Aug-31-2023, 05:43 PM)deanhystad Wrote: When you make the buttons you need to tell them their parent is "window". For example:
deposit_button=Button(window, text="Deposit Money" image=deposit_img, command=deposit_money)
But that is not your problem. As I said, the default is put the widgets in the root window.

The problem is this:
bank_pic=PhotoImage(file="./bank.png")  # Works
withdraw_img=PhotoImage("./withdraw.jpg")  # Does not work
See the difference? No, it is not the image type.
You are a boss. Thanks a lot again
Reply
#8
The first positional argument for PhotoImage() is name, a string that is used to tag the image. If you don't provide a name, one is generated for you, like "pyimage1". file is a named argument. Your code was creating empty images named "./withdraw.jpg" and "./deposit.jpg".
lunacy90 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to open a popup window in tkinter with entry,label and button lunacy90 1 901 Sep-01-2023, 12:07 AM
Last Post: lunacy90
Bug tkinter.TclError: bad window path name "!button" V1ber 2 805 Aug-14-2023, 02:46 PM
Last Post: V1ber
  Blending images with tkinter rleiman 0 1,055 Apr-15-2022, 08:37 PM
Last Post: rleiman
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 1,715 Jan-08-2022, 12:13 PM
Last Post: law
  tkinter auto press button kucingkembar 2 3,195 Dec-24-2021, 01:23 PM
Last Post: kucingkembar
  MP3 Player Images Not Showing Harshil 2 1,793 Aug-18-2020, 01:54 PM
Last Post: Harshil
  Problem using a button with tkinter SmukasPlays 6 3,313 Jul-02-2020, 08:06 PM
Last Post: SmukasPlays
  Use a button in Tkinter to run a Python function Pedroski55 4 3,276 Jun-28-2020, 05:02 AM
Last Post: ndc85430
  Blender 2.49b not showing button when I run script on ubuntu like it does in windows? Vido 0 2,680 Dec-13-2019, 05:56 PM
Last Post: Vido
  Function assigned at a button in tkinter riccardoob 9 4,192 Oct-06-2019, 11:14 AM
Last Post: riccardoob

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020