简体   繁体   English

PyQt5 中的 Tkinter 窗口、按钮和显示 HTML 页面

[英]Tkinter window, button and display HTML page in PyQt5

Good morning/evening everyone, I'm trying to create GUI with Tkinter and display HTML page with PyQt5.QtWebEngineWidgets after click Tkinter button.大家早上好/晚上好,我正在尝试使用 Tkinter 创建 GUI,并在单击 Tkinter 按钮后使用 PyQt5.QtWebEngineWidgets 显示 HTML 页面。 Everything works fine, but after close PyQt HTML page and again click button - everything freezes and kernel is restarting.一切正常,但在关闭 PyQt HTML 页面并再次单击按钮后 - 一切都冻结了,内核正在重新启动。

I post the most important code below:我在下面发布了最重要的代码:

import tkinter as tk    

window = tk.Tk()
btn = tk.Button(window, text = "Click me!",  command = display_HTML)
btn.place(relx=0.2, rely=0.6, anchor="center")
window.mainloop()

And display_HTML function:和 display_HTML 功能:

from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
import sys

def display_HTML():
    app = QApplication(sys.argv)
    
    br = QWebEngineView()
    file_path = r"C:\....html"
    local_url = QUrl.fromLocalFile(file_path)
    br.load(local_url)
    
    br.setWindowTitle("HTML content")
    br.show()
    
    app.exec_()       

Could you help me to resolve this issue?你能帮我解决这个问题吗? Thank you very much.非常感谢。

Note: When I press the button and the HTML viewer is launched, I observe that the window containing the button freezes (to check this, try to change the size of that window), and when the HTML viewer is launched again, a Segmentation fault (core dumped) .注意:当我按下按钮并启动 HTML 查看器时,我观察到包含该按钮的窗口冻结(要检查这一点,请尝试更改该窗口的大小),并且当再次启动 HTML 查看器时,出现分段错误(核心转储)

2 eventloops (tkinter and Qt) cannot coexist in the same thread since they are blocked, so a possible solution is to launch Qt in a new process: 2 个事件循环(tkinter 和 Qt)不能在同一个线程中共存,因为它们被阻塞了,所以一个可能的解决方案是在一个新进程中启动 Qt:

import tkinter as tk
from multiprocessing import Process

window = tk.Tk()
btn = tk.Button(
    window, text="Click me!", command=lambda *args: Process(target=display_HTML).start()
)
btn.place(relx=0.2, rely=0.6, anchor="center")
window.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM