Python Tkinter 表达式未显示在第二个 GUI 屏幕上

问题描述 投票:0回答:1

我有 2 个不同的 .py 文件,一个具有主 Tkinter GUI 窗口,另一个具有在 tkinter GUI 中运行计算器的代码,计算器文件单独执行时工作正常,但在主窗口内调用时,不显示任何表达式主 GUI 只是按钮。

主要文件


import tkinter as tk
from tkinter import ttk
from tkinter import *
import cv2
from PIL import Image, ImageTk


# Function to launch a placeholder camera app
def launch_camera():
    #from totestvideocapture import openCameraMain
    '''
    root = tk.Tk()
    camera_app = CameraApp(root)
    root.protocol("WM_DELETE_WINDOW", lambda: on_closing_camera(root, camera_app))  # Handle window close
    root.mainloop()
    '''

class App:
    def __init__(self, root):
        from totestvideocapture import CameraThread
        self.root = root
        root.title("Tkinter with OpenCV")
        self.label = tk.Label(root, text="Tkinter window")
        self.label.pack()

        # Start the camera thread
        self.camera_thread = CameraThread(self)
        self.camera_thread.start()



def launch_calculator():

    from Calculator_App import CalculatorGUI
    if __name__ == "__main__" :
        root = Tk()
        calculator = CalculatorGUI(root)
        root.mainloop()

    
root = tk.Tk()
root.title("Simple GUI")
root.geometry ("1000x800") # Default WidthxHeight
root.minsize(200,300) # locks the window size so that it can't go under these Width, Height
root.maxsize(1920,1080) # locks the window size so that it can't go over these Width, Height 


style1 = ttk.Style()
style1.configure("Frame1.TFrame", background="lightblue")
frame_interaction = ttk.Frame(root, style="Frame1.TFrame",padding=10, relief="ridge")
frame_interaction.place(x=0, y=0, width = 1536, height = 540)


style2 = ttk.Style()
style2.configure("Frame2.TFrame", background="lightgreen")
frame_face = ttk.Frame(root, style="Frame2.TFrame",padding=10, relief="ridge")
frame_face.place(x=936, y=0, width = 600, height = 250)

style3 = ttk.Style()
style3.configure("Frame3.TFrame", background="lightgreen")
frame_Games = ttk.Frame(root, style="Frame3.TFrame",padding=10, relief="ridge")
frame_Games.place(x=0, y=540, width = 768, height = 540)


style4 = ttk.Style()
style4.configure("Frame4.TFrame", background="red")
frame_VA = ttk.Frame(root, style="Frame4.TFrame",padding=10, relief="ridge")
frame_VA.place(x=768, y=540, width = 768, height = 540)



style5 = ttk.Style()
style5.configure("Frame5.TFrame", background="purple")
frame_Rem = ttk.Frame(root, style="Frame5.TFrame",padding=10, relief="ridge")
frame_Rem.place(x=1536, y=0, width = 384, height = 320)


style6 = ttk.Style()
style6.configure("Frame6.TFrame", background="black")
frame_Health_Data = ttk.Frame(root, style="Frame6.TFrame",padding=10, relief="ridge")
frame_Health_Data.place(x=1536, y=320, width = 384, height = 760)



camera_button = ttk.Button(frame_interaction, text="Interaction")
camera_button.place(relx=0.5, rely=0.5, anchor="center")

camera_button = ttk.Button(frame_face, text="Face")
camera_button.place(relx=0.5, rely=0.5, anchor="center")

camera_button = ttk.Button(frame_Games, text="Games")
camera_button.place(relx=0.5, rely=0.5, anchor="center")

camera_button = ttk.Button(frame_Rem, text="Rem")
camera_button.place(relx=0.5, rely=0.5, anchor="center")

camera_button = ttk.Button(frame_VA, text="VA")
camera_button.place(relx=0.5, rely=0.5, anchor="center")

camera_button = ttk.Button(frame_Health_Data, text="Health Data")
camera_button.place(relx=0.5, rely=0.5, anchor="center")


'''

frame = ttk.Frame(root, padding=10)
frame.grid(column=0, row=0, sticky=(tk.N, tk.S, tk.W, tk.E))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)


frame = ttk.Frame(root, padding=10)
frame.grid(column=0, row=0, sticky=(tk.N, tk.S, tk.W, tk.E))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

'''


# Button to launch the camera
camera_button = ttk.Button(frame_interaction, text="Camera",command=launch_calculator)
camera_button.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E))


# Start the main event loop
if __name__ == "__main__":
    root.mainloop()

计算器.py



from tkinter import *

class CalculatorGUI:
    def __init__(self, GUI):
        self.equation = StringVar()
        self.expression = ""
        self.equation.set('enter your expression')
        GUI.configure(background="light green") 
        GUI.title("Simple Calculator") 
        GUI.geometry("265x125") 



        self.expression_field = Entry(GUI, textvariable=self.equation)
        self.expression_field.grid(columnspan=4, ipadx=70)

        buttons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0',]

        row_val = [2, 2, 2, 3, 3, 3, 4, 4, 4, 5]
        col_val = [0, 1, 2, 0, 1, 2, 0, 1, 2, 0]

        for button_text, row, col in zip(buttons, row_val, col_val):
            button = Button(GUI, text=f' {button_text} ', fg='black', bg='red',
                            command=lambda btn=button_text: self.press(btn), height=1, width=7)
            button.grid(row=row, column=col)

        symbol_buttons = ['+', '-', '*', '/']
        for symbol in symbol_buttons:
            button = Button(GUI, text=f' {symbol} ', fg='black', bg='red',
                            command=lambda sym=symbol: self.press(sym), height=1, width=7)
            button.grid(row=symbol_buttons.index(symbol) + 2, column=3)

        equal = Button(GUI, text=' = ', fg='black', bg='red',
                       command=self.equalpress, height=1, width=7)
        equal.grid(row=5, column=2)

        clear_btn = Button(GUI, text='Clear', fg='black', bg='red',
                           command=self.clear, height=1, width=7)
        clear_btn.grid(row=5, column=1)

    def press(self, num):
        self.expression = self.expression + str(num)
        self.equation.set(self.expression)

    def equalpress(self):
        try:
            result = str(eval(self.equation.get()))
            self.equation.set(result)
            self.expression = ""

        except:
            self.equation.set(" error ")
            self.expression = ""
    
    def clear(self):
        self.expression = ""
        self.equation.set("")


if __name__ == "__main__":
    root = Tk()
    calculator = CalculatorGUI(root)
    root.mainloop()

我希望在其中一个颜色编码框架上运行计算器应用程序并制作一个类似 GUI 的窗口。

python user-interface tkinter
1个回答
0
投票

当您有两个窗口

Tk()
和两个
mainloop()
时,在所有元素中设置父级(甚至在
StringVar()
中)非常重要,以显示哪个窗口应与这些元素一起使用。
如果您没有它,那么它可能会在错误的窗口下工作,并且您可能无法得到结果。

你需要

StringVar(GUI)
CalculatorGUI

© www.soinside.com 2019 - 2024. All rights reserved.