打开两扇窗户而不是一扇,如果我指的是根风,会崩溃

2024-05-12 12:43:59 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行我的程序时,它会产生两个窗口。一个叫Tk,一个叫这是我的头衔。它在有标题的窗口中绘制组合框和标签,但在Tk窗口中绘制图像标签。你知道吗

现在我知道我需要在图像标签中指定(window,image=photo),以便它在标题窗口中绘制…但是如果这样做,我会遇到崩溃,它的内容如下:

错误:

<PIL.Image.Image image mode=RGBA size=960x540 at 0x19B6AFD6948>
About to set Photo for first time
Class Photo is: pyimage1
Traceback (most recent call last):
  File "t:/Highlight/ImageDetect_Py/ImageDetect/New_Test.py", line 54, in <module>
    photoLabel = Label(window, image=photo)
  File "C:\Users\Tommy\Anaconda3\envs\OCV\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\Tommy\Anaconda3\envs\OCV\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist
Exception ignored in: <function BaseMySQLSocket.__del__ at 0x0000019B691E2828>
Traceback (most recent call last):
  File "C:\Users\Tommy\Anaconda3\envs\OCV\lib\site-packages\mysql\connector\network.py", line 133, in __del__
  File "C:\Users\Tommy\Anaconda3\envs\OCV\lib\site-packages\mysql\connector\network.py", line 121, in shutdown
TypeError: catching classes that do not inherit from BaseException is not allowed
Exception ignored in: <function BaseMySQLSocket.__del__ at 0x0000019B691E2828>
Traceback (most recent call last):
  File "C:\Users\Tommy\Anaconda3\envs\OCV\lib\site-packages\mysql\connector\network.py", line 133, in __del__
  File "C:\Users\Tommy\Anaconda3\envs\OCV\lib\site-packages\mysql\connector\network.py", line 121, in shutdown
TypeError: catching classes that do not inherit from BaseException is not allowed

工作代码(打开两个窗口,Tk和标题):

import time #Import Time
import concurrent.futures #Import for multithreading
from concurrent.futures import ThreadPoolExecutor
from multiprocessing import Process, pool  #Import for multiprocessing
from tkinter import Label, Entry, Button, W, E, S, N, NW, NE, SW, SE, Tk, Canvas, ttk, mainloop
from PIL import Image, ImageTk, ImageDraw #Import Pillow for TKInter image conversion
import sys
import urllib.request
import threading
import queue
import numpy as numpy
from pathlib import Path

#Layout GUI
window = Tk()
window.title('This is my title')
width_of_window = 1280
height_of_window = 720
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
tk_x_cord = (screen_width/2) - (width_of_window/2)
tk_y_cord = (screen_height/2) - (height_of_window/2)
window.geometry('%dx%d+%d+%d' % (width_of_window, height_of_window, tk_x_cord, tk_y_cord))
window.resizable(width=False, height=False)

#Callback function for combo boxes
def callbackFunc(event):
    print('New Element Selected')

#Camera Select Box
cameraComboLabel = Label(window, text='Choose Camera:')
cameraComboLabel.grid(column=1, row=1, padx=25, pady=20, sticky=W+N)
cameraComboBox = ttk.Combobox(window, values=["Camera1", "Camera2"], state='readonly')
cameraComboBox.grid(column=2, row=1, padx=1, pady=20, sticky=W+N)
cameraComboBox.current(0)
cameraComboBox.bind("<<ComboboxSelected>>", callbackFunc)

#Load last saved snapshot as placeholder image
lastImage = Path('snapShots/192.168.0.13.jpg')
image1 = Image.open(lastImage)
image1 = image1.resize((960,540))
image1 = image1.convert('RGBA')
print(image1)
print('About to set Photo for first time')
photo = ImageTk.PhotoImage(image1) #Store Tk image for use in GUI
print(f'Class Photo is: {photo}')

#Create image label
photoLabel = Label(image=photo) #If I type Label(window,image=photo) it crashes
photoLabel.grid(column=3, row=5, padx=25, pady=5, sticky=W+S)


window.mainloop()

提前谢谢!:D个


Tags: infrompyimageimportforlinetommy