【问题标题】:how to hide the title bar or window in python如何在python中隐藏标题栏或窗口
【发布时间】:2023-03-16 18:10:01
【问题描述】:

我想隐藏我的窗口或窗口的标题栏。因为我想显示opencv无框的输出。

告诉我如何在 tkinter 或 pyqt5 的帮助下做到这一点。 而且我想要根据我的规范输出的大小。 并且我可以在代码运行时调整输出的大小和更改输出的位置,例如在桌面上的任何位置拖放。

import cv2
import os
import numpy as np
import datetime

date=datetime.datetime.now()
framerate=24
screen_size=(640,480) #std res 640,480
filename='E:/project/videos/cam_%s%s%s%s%s%s.avi' %(date.year,date.month,date.day,date.hour,date.minute,date.second)
fourcc = cv2.VideoWriter_fourcc(*'XVID')

cap=cv2.VideoCapture(0)
out= cv2.VideoWriter(filename,fourcc,framerate,screen_size)
cv2.namedWindow('frame',cv2.WINDOW_NORMAL)
cv2.resizeWindow('frame',280,190)
cv2.moveWindow('frame',5,495)
#cv2.namedWindow('frame',cv2.WND_PROP_FULLSCREEN)   #use for full screen
#cv2.setWindowProperty('frame', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)



while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:

        frame=cv2.flip(frame,1)


        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break       



cap.release()
out.release()
cv2.destroyAllWindows()
screen.mainloop()

【问题讨论】:

  • 在tkinter中,在窗口上调用.overrideredirect(1)来移除窗口装饰。
  • @acw1668 当我这样做时,会打开两个不同的窗口,一个是网络摄像头,另一个是 tkinter 窗口。
  • 请更新您发布的代码以包含tkinter 部分。
  • @acw1668 代码已更新,请说明我哪里出错了,如果您愿意,可以自己编辑上述代码。
  • 首先您不应该看到tkinter 窗口,因为screen.mainloop() 由于while 循环而不会被调用。如果您按 'q' 终止 while 循环,它将被调用。如果你想使用tkinter窗口,你也不应该使用cv2.imshow(...)

标签: python python-3.x opencv tkinter pyqt5


【解决方案1】:

以下是使用tkinter 显示来自网络摄像头的捕获图像的示例:

import cv2
import datetime
from tkinter import *
from PIL import Image, ImageTk

screen = Tk()
screen.overrideredirect(1)  # remove window decorations
screen.geometry('280x200+5+520')  # move the window

framerate = 24
screen_size = (640, 480) #std res 640,480

date = datetime.datetime.now()
filename = 'E:/project/videos/cam_%s%s%s%s%s%s.avi' % (date.year, date.month, date.day,
                                                       date.hour, date.minute, date.second)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(filename, fourcc, framerate, screen_size)

cap = cv2.VideoCapture(0)

imgbox = Label(screen)
imgbox.pack(fill=BOTH, expand=1)

def quit(event):
    cap.release()
    out.release()
    screen.destroy()

def read_frame():
    ret, frame = cap.read()
    if ret:
        frame = cv2.flip(frame, 1)
        out.write(frame)
        # since openCV capture image is in BGR color sequence
        # so need to convert it to RGB color sequence
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        # convert the openCV image to PIL (Pillow) format
        img = Image.fromarray(frame)
        #img = img.resize((280, 200))  # not keep aspect ratio
        img.thumbnail((280, 200))  # keep aspect ratio
        img = ImageTk.PhotoImage(img)
        imgbox.config(image=img)  # update the image
        img.image = img  # keep a reference of the image to avoid being garbage collected
    screen.after(20, read_frame)  # schedule next read after 20 ms

# bind the 'q' key to quit program
screen.bind('q', quit)

read_frame()
screen.mainloop()

【讨论】:

  • 感谢您的解决方案,但我想要screen.geometry("280x200+5+520") # move the window,当我这样做时,我只看到网络摄像头的一部分而不是整个网络摄像头?
  • 您需要调整图像大小以适应窗口。
  • 我该怎么做?
  • @amit 答案已更新。您可以使用resize(),它不会保持纵横比而是填充整个窗口。或者 thumbnail() 保持纵横比但可能不会填满整个窗口。
  • 我很高兴你帮了我这个忙,我终于实现了我想要的。你能帮我解决这个代码更新的问题吗?我正在尝试用 pyqt5 或 tkinter 做同样的事情,任何人都会做。在我的屏幕录制代码中。
猜你喜欢
  • 1970-01-01
  • 2012-05-31
  • 2021-11-04
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
相关资源
最近更新 更多