簡體   English   中英

python導入文件但無法識別圖像

[英]python importing file but image not recognized

我創建了一個帶有下拉菜單的 python 文件。 當我選擇選項之一時,它會導入另一個 python 文件,在畫布中帶有一個復選按鈕和一張圖片。 文件和圖片都位於同一文件夾中。 代碼導入文件導入畫布和復選按鈕,但我收到錯誤消息,說圖像“pyimage1”不存在。 如果我單獨運行第二個文件,它會顯示復選按鈕和圖像而沒有錯誤。 導入 python 文件時,圖像不再被識別,或者我做錯了什么? 有什么解決方法嗎?

主程序:

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()

要導入的文件:

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()

更新了導入函數的代碼:

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()

要導入的函數在哪里的文件:

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()

這是我知道如何在 tkinter 中導入文件和函數的方式,不確定這是否正確,但請查看我對這兩個文件所做的更改

主要.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()

和函數.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()

希望對大家有所幫助,如有任何疑問或錯誤,請告訴我。

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM