-1

I tried to make a module in which I made a funtion which just reads and display the image in GUI. Then I made another module which makes call to that function when the button is clicked. Button gives me error.

#module code:
from tkinter import *
class disp:
    def __init__(self):
        root1.geometry("400x500")
        image = PhotoImage(file = 'png2.png')
        Label(root1,image=image).pack()
        root1.mainloop()


#main code:
from tkinter import *
import testimg as ti

def click():
    ti.disp()

root = Tk()

Button(text = 'Click me',command=click).pack()
root.mainloop()

2 Answers2

0

In your class disp, you have put the master as root1 whereas in the main code, you have defined Tk() as root. This means that root1 is no window so the label that has a master of root1 has no where to pack itself.

You also need to remove root1.mainloop() because it’s useless and causing errors due to the fact that root1 doesn’t have Tk(). It’s like trying to loop a while statement without typing in while. This gives an error.

hifromdev
  • 262
  • 3
  • 10
0

Below modified code is based on yours:

#module code:
from tkinter import *

class disp:
    def __init__(self):
        root1 = Tk()
        root1.geometry("400x500")
        image = PhotoImage(master=root1, file='png2.png') # set master to root1
        Label(root1, image=image).pack()
        root1.mainloop()

But using multiple Tk() instances is not a good design.

acw1668
  • 40,144
  • 5
  • 22
  • 34