0

I have created one python file with a drowdown menu. When i choose the option one, it imports another python file, with a checkbutton and a picture in a canvas. Both files and the picture are located in the same folder. The code import the file imports the canvas and the checkbutton, but I get the error saying image "pyimage1" doesn't exist. If I run that second file alone, it does show the checkbutton and the image without errors. When Import a python file the images are not recognized anymore or am I doing something wrong? is any workaround there?

main program:

from tkinter import *

root = Tk()
root.geometry('1560x750')

canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)

def option_number(x):
    if x == "one":
        import part2

variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)

root.mainloop()

file to be imported:

from tkinter import *

root = Tk()
root.geometry('1560x750')

canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)

button = Checkbutton(canvas).place(x=170, y=230)
AND_gate=PhotoImage(file='AND.png') #set up variables for and_gate
labelimage_and = Label(canvas, image=AND_gate).place(x=200,y=200)

root.mainloop()

Updated code to import function:

from tkinter import *

root = Tk()
root.geometry('1560x750')

canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)

def option_number(x):
    if x == "one":
        from part1 import import_def

variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)

root.mainloop()

file where is the function to be imported:

from tkinter import *
root = Tk()

def import_def():
    root = Toplevel()
    root.geometry('1560x750')
    canvas2 = Canvas(root)
    canvas2.config(width=1000, height=1560, bg='red')
    canvas2.grid(row=1, column=3, rowspan=1550, ipadx=1300, ipady=750, sticky=NW)

    button = Checkbutton(canvas2).place(x=170, y=230)
    AND_gate=PhotoImage(file='AND.png') #set up variables for and_gate
    labelimage_and = Label(canvas2, image=AND_gate).place(x=200,y=200)

root.mainloop()
10
  • 1
    That is because you are having two instance of Tk() try changing one to Toplevel() Commented Aug 28, 2020 at 19:23
  • Thank you, if I change the imported file to Toplevel() it works, (not if i change the "main file". However, choosing "one" it opens a new window with the program, but if I close that window and choose "one" again, it doesn't open anymore. Any way to make it keep opening when I choose "one"? Commented Aug 28, 2020 at 19:46
  • i would recommend to put everything isnide the 2nd file in a function and import the function and run that function instead Commented Aug 28, 2020 at 19:50
  • But I need it to be in a different window so I can close it when I'm done with it, but available it I want to do it again. Commented Aug 28, 2020 at 19:58
  • Ya i think its possible with what i just said Commented Aug 28, 2020 at 20:03

1 Answer 1

1

This is the way that I know on how to import files and functions within tkinter, not sure if this is the right way but take a look at the changes I made to both the files

main.py:

from tkinter import *
from function import import_def

root = Tk()
root.geometry('1560x750')

canvas=Canvas(root)
canvas.config(width=1000, height=1560, bg='light grey')
canvas.grid(row=1,column=3, rowspan=1550,ipadx=1300,ipady=750,sticky=NW)

def option_number(x):
    if x == "one":
        import_def()

variable = StringVar()
variable.set("options")
w = OptionMenu(canvas, variable, "one", "two",command = option_number)
w.config(width=15, height=1,bg='blue')
w.place(x=400,y=100)

root.mainloop()

and function.py:

from tkinter import *

def import_def():
    root = Toplevel()
    root.geometry('1560x750')
    canvas2 = Canvas(root)
    canvas2.config(width=1000, height=1560, bg='red')
    canvas2.grid(row=1, column=3, rowspan=1550, ipadx=1300, ipady=750, sticky=NW)

    button = Checkbutton(canvas2).place(x=170, y=230)
    AND_gate=PhotoImage(file='sad songs.jpg') #set up variables for and_gate
    labelimage_and = Label(canvas2, image=AND_gate).place(x=200,y=200)

    root.mainloop()

Hope it was of some help, do let me know if any doubts or errors.

Cheers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.