0

i have 2 python file TestBtnImage1.py and TestBtnImage2.py

I am trying to call display() fucntion of TestBtnImage2 on button click of TestBtnImage1.py, button without image displying in frame, however button with image is not displying. thanks in advance

TestBtnImage1.py

from tkinter import *
from TestBtnImage1 import *

frame = Tk()
frame.title("TextBox Input")
frame.geometry('200*200')
frame.config(background="blue")

printButton = Button(frame, text="click me", command=display()).place(x=50, y=50)
frame.mainloop()

TestBtnImage2.py

from tkinter import *


def display():
    frame = Tk()
    frame.title("TextBox Input")
    frame.geometry('200*200')
    frame.config(background="blue")

    logo1 = PhotoImage(file="cheta.png")
    Button(gui, text="click me", image=logo1, bg="blue",  relief=FLAT).place(x=100, y=200)

    frame.mainloop()

tried googling for the same issue also stackoverflow but no help.

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Nov 05 '22 at 09:14
  • First `command=display()` will execute `display()` immediately without clicking the button. `command=display` should be used instead. Second don't create more than one instance of `Tk()`, use `Toplevel()` for windows other than the root window. Also run `.mainloop()` only once. The image issue is the same as this [question](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function). – acw1668 Nov 05 '22 at 16:00
  • As I can see in `TestBtnImage1.py`, you are importing itself (`from TestBtnImage1 import *`). It should be `from TestBtnImage2 import *`, right? Also, @acw1668 has already explained, use `command=display` instead of `display()`... – Hung Nov 05 '22 at 16:27

1 Answers1

0

There are a few problems in your code.

In TestBtnImage1.py

  • You are importing itself, which will cause a NameError.
    from TestBtnImage1 import *
    and you just need to change it to TestBtnImage2

  • The geometry specifier should be 'x'
    If you use frame.geometry('200*200'), it will cause tkinter bad geometry specifier error.
    So use 200x200 instead of 200*200.

  • The button command
    printButton = Button(frame, text="click me", command=display()).place(x=50, y=50)
    The display() function will automatically run when executed. You can remove the brackets ()
    -> printButton = Button(frame, text="click me", command=display).place(x=50, y=50)

(*) Also, the printButton (None) does nothing. So you can remove it.

In TestBtnImage2.py

  • Use Toplevel() instead of a new Tk()
    -> frame = Toplevel()

  • Again, bad geometry specifier. -> frame.geometry('200x200')

  • Use mainloop() only once. You can remove frame.mainloop() at the end of the function

(*) Also, Button(gui, text="click me", image=logo1, bg="blue", relief=FLAT).place(x=100, y=200)
The master gui is not defined.



So, the full code should be:
TestBtnImage1.py:

from tkinter import *
from TestBtnImage2 import *

frame = Tk()
frame.title("TextBox Input")
frame.geometry('200x200')
frame.config(background="blue")

Button(frame, text="click me", command=display).place(x=50, y=50)
frame.mainloop()

TestBtnImage2.py:

from tkinter import *


def display():
    frame = Toplevel()
    frame.title("TextBox Input")
    frame.geometry('200x200')
    frame.config(background="blue")

    logo1 = PhotoImage(file="cheta.png")
    Button(frame, text="click me", image=logo1, bg="blue",  relief=FLAT).place(x=100, y=200)
Hung
  • 155
  • 10