使用Python Arcade和Tkinter一起挂应用

问题描述

我正在尝试构建一个生成动态视觉模型的应用程序(在 Windows 上使用 Python 3 和 PyCharm)。我使用 Arcade 作为主查看/用户交互窗口,使用 Tkinter 进行初步数据输入、模型参数、数值输出错误和警告等。

我发现如果在 Arcade 窗口打开时打开 tk 窗口(例如 messageBox.showinfo 或 messageBox.error),应用程序会挂起。这是重现问题的最小片段:

import tkinter.messageBox
import arcade

tkinter.messageBox.showinfo("Greetings","hello")
app = arcade.Window(500,300,"Let's play")
tkinter.messageBox.showinfo("Greetings","hello again")

第二个消息框永远不会打开,高达 30% 的 cpu 处于活动状态,而 Python 除了(理论上)等待用户输入之外什么都不做。

解决方法

您可以从 tkinter 启动 arcade 应用:

import arcade
import tkinter as tk

class ArcadeApp(arcade.Window):
    def __init__(self):
        super().__init__(400,300)
        self.root = None

    def on_closing(self):
        self.root.destroy()
        self.root = None

    def on_draw(self):
        arcade.start_render()
        arcade.draw_text('Click to launch Tkinter',200,150,arcade.color.RED,30,align='center',anchor_x='center')

    def on_mouse_release(self,x,y,button,key_modifiers):
        if not self.root:
            self.root = tk.Tk()
            self.root.geometry('400x300')
            self.root.protocol('WM_DELETE_WINDOW',self.on_closing)
            label = tk.Label(self.root,text='Greetings from Tkinter!')
            label.config(font=('',20))
            label.place(relx=0.5,rely=0.5,anchor='center')
            self.root.mainloop()

ArcadeApp()
arcade.run()

输出:

Arcade

Tkinter