Open GustaCruz opened 3 weeks ago
import cv2 import threading from tkinter import Frame, Label, Tk from PIL import Image, ImageTk import customtkinter as ctk
ctk.set_appearance_mode("dark") ctk.set_default_color_theme("dark-blue")
class App(ctk.CTk):
APP_NAME = "Drone System with Webcam"
WIDTH = 800
HEIGHT = 450
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title(App.APP_NAME)
self.geometry(f"{App.WIDTH}x{App.HEIGHT}")
self.minsize(App.WIDTH, App.HEIGHT)
self.protocol("WM_DELETE_WINDOW", self.on_closing)
# Create main frame
self.frame = ctk.CTkFrame(master=self)
self.frame.pack(pady=10, padx=6, fill="both", expand=True)
self.label = ctk.CTkLabel(master=self.frame, text="Drone System", font=("Roboto", 14), height=0)
self.label.pack(pady=1, padx=10, side="top", anchor="n")
# Main frame for webcam
self.main_frame = Frame(master=self.frame, width=500, height=350, background="black")
self.main_frame.pack(pady=1, padx=8, side="top", anchor="ne", fill="both", expand=True)
self.webcam_label = Label(self.main_frame)
self.webcam_label.pack() # Pack the webcam label correctly
# Create navigation buttons at the bottom
self.nav_buttons_frame = ctk.CTkFrame(master=self.frame, height=50, corner_radius=0)
self.nav_buttons_frame.pack(side="bottom", fill="x")
# Create buttons with padding between them
self.create_nav_button("MAIN", self.switch_to_webcam)
self.create_nav_button("MAP", self.switch_to_map)
self.create_nav_button("STAT", self.switch_to_stat)
self.create_nav_button("SNAP", lambda: print("SNAP clicked"))
self.create_nav_button("SETT", lambda: print("SETT clicked"))
self.create_nav_button("QUIT", self.on_closing)
# Start webcam thread
self.thread = threading.Thread(target=self.show_webcam)
self.thread.daemon = True
self.thread.start()
def create_nav_button(self, text, command):
button = ctk.CTkButton(
master=self.nav_buttons_frame,
text=text,
command=command,
width=80, # Set a fixed width for the buttons
height=30 # Optionally, set a fixed height
)
button.pack(side="left", padx=6, pady=4) # Increased padding
def switch_to_webcam(self):
self.webcam_label.pack()
# Hide other frames or features if any
def switch_to_map(self):
# Code to switch to map view
self.webcam_label.pack_forget()
def switch_to_stat(self):
# Code to switch to stat view
self.webcam_label.pack_forget()
def show_webcam(self):
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (580, 350))
img = Image.fromarray(frame)
imgtk = ImageTk.PhotoImage(image=img)
self.webcam_label.configure(image=imgtk)
self.webcam_label.image = imgtk
if cv2.waitKey(1) == ord('q'):
break
cap.release()
def on_closing(self):
self.destroy()
if name == "main": app = App() app.mainloop()
File "/usr/lib/python3.11/tkinter/init.py", line 1948, in call return self.func(args) ^^^^^^^^^^^^^^^^ File "/home/SPOTS/myenv/lib/python3.11/site-packages/customtkinter/windows/widgets/ctk_button.py", line 554, in _clicked self._command() File "/home/SPOTS/Documents/projects/openvc2-project/test2.py", line 115, in switch_to_webcam self.webcam_label.pack() File "/usr/lib/python3.11/tkinter/init.py", line 2452, in pack_configure self.tk.call( _tkinter.TclError: cannot use geometry manager pack inside .!ctkframe.!frame which already has slaves managed by grid ^CTraceback (most recent call last): File "/home/SPOTS/Documents/projects/openvc2-project/test2.py", line 188, in
app.mainloop()
File "/home/SPOTS/myenv/lib/python3.11/site-packages/customtkinter/windows/ctk_tk.py", line 165, in mainloop
def call(self, args):inter/init.py", line 1943, in call