我也尝试将两个窗口分别作为面板A和panelB放置,但出现以下错误
如果PanelA为None,则为PanelB;如果为None,则为PanelB:NameError:name'panelA'不是 定义
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
import cv2
def select_image():
global panelA, panelB
path = filedialog.askopenfilename()
if len(path) > 0:
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 50, 100)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
edged = Image.fromarray(edged)
image = ImageTk.PhotoImage(image)
edged = ImageTk.PhotoImage(edged)
if panelA is None or panelB is None:
panelA = Label(image=image)
panelA.image = image
panelA.pack(side="left", padx=10, pady=10)
panelB = Label(image=edged)
panelB.image = edged
panelB.pack(side="right", padx=10, pady=10)
else:
panelA.configure(image=image)
panelB.configure(image=edged)
panelA.image = image
panelB.image = edged
root = Tk()
panelA = None
panelB = None
btn = Button(root, text="Select an image", command=select_image)
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
root.mainloop()
答案 0 :(得分:1)
您定义了全局变量,但是如果要在if语句中进行比较,还需要对其进行初始化:
global panelA, panelB
panelA, panelB = None, None
答案 1 :(得分:0)
请修复您的缩进。panelA,panelB =''