Pyside:addToJavaScriptWindowObject在用cython编译应用程序时无法正常工作

2024-09-30 16:28:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经构建了一个运行几千行代码的应用程序。在

我试图通过将python代码编译成c代码,然后再编译到一个目标文件来隐藏代码。在

这个应用程序在pyside中运行良好。我将这个python文件py转换为.c文件,并编译了c-to和目标文件.o。此文件的名称为test2.py,我将其转换为.o文件的步骤:

 cython --embed -o test2.c test2.py

根据上面的内容,我现在将test2.c编译为test2.o,如下所示

^{pr2}$

我的问题是,在webkit框架中设计的minimize and close按钮在编译后不起作用(但是从python文件执行时效果很好),其他addtojavascripwindowbject也不工作

我用Pyside开发了一个简单的框架窗口模型,里面什么也没有

test2.py文件代码如下: 我的密码在下面

from __future__ import print_function
import sys
import json
import threading
from time import sleep
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import QWebView, QWebSettings
from PySide.QtNetwork import QNetworkRequest

title_bar_str="""<!doctype html>
<html>
<body>

<section id="exit_panel">
    <div id="window_title_meta_container">
        <img id="window_title_icon" draggable="false">
        <div id="window_title"></div>
    </div>
    <div id="min_close_container">
    <div id="min_ap" class="win_signals">_</div>
    <div id="close_ap" class="win_signals">x</div>
    </div>
</section>
<style>
img#window_title_icon{
    display: none;
    width:25px;
    height:25px;
    vertical-align:top;
    border:none;
}
#window_title_meta_container{
    float:left;
    color:grey !important;
}
#window_title_meta_container,#window_title_icon,#window_title{
    display:inline-block;
}
#title_bar{
    color:black;
    background: -webkit-linear-gradient(blue,grey);
    font-size:15px;
}
.btns{
    position:relative;
    display:inline-block;
    padding:5px;
    border:solid red 1px;
    background:red;
    left:85%
}
body{
    border:solid black 1px;
    height:100%;
    margin:auto;
    background:red;
    -webkit-user-select:none;
    -moz-user-select:none;

}
body:hover{
    cursor:pointer;
}
html,body{
    overflow:hidden
}
.win_signals{
    display:inline-block;
    padding:5px;

}
#close_ap,#min_ap{
color: white;
background: rgb(18, 110, 201) none repeat scroll 0% 0%;
border-radius: 10px;            
}
#close_ap:hover,#min_ap:hover{
    cursor:pointer;
    background:green;
}
#min_close_container{
position: relative;;
display: inline-block;
}

#exit_panel{
    text-align: right;
    background: -webkit-linear-gradient(#104477, #000) repeat scroll 0% 0% transparent;
    color: white;
}
</style>
<script>
window.onload=function(){
    document.getElementById('close_ap').onclick=function(){
        console.log(trigger_title_signals.close(''))
    }
    document.getElementById('min_ap').onclick=function(){
        console.log(trigger_title_signals.minimize(''))
    }    


}

</script>
</body>
</html>
</doctype>

"""
class Frame(QFrame,QObject):
    def __init__(self):
        super(Frame,self).__init__()
        self.gui_height=None
        self.title_bar_height=None
        self.html_title_bar_webview=web_view_with_move(self)
        self.html_title_bar_webview.init_signals()
        self.html_title_bar_webview.setContent(title_bar_str)
        self.html_title_bar_webview.page().mainFrame().setScrollBarPolicy(Qt.Vertical,Qt.ScrollBarAlwaysOff)
        self.html_title_bar_webview.page().mainFrame().setScrollBarPolicy(Qt.Horizontal,Qt.ScrollBarAlwaysOff)
        self.html_title_bar_webview.page().mainFrame().addToJavaScriptWindowObject("Frame",self)      
        self.title_bar_q_widget=title_bar_q()
        self.title_bar_q_widget.setMaximumHeight(35)
        self.title_bar_q_widget.setMinimumHeight(35)
        self.title_bar_q_widget.setContentsMargins(0,0,0,0)
        #self.title_bar_q_widget.layout.setSpacing(0)
        self.title_bar_q_widget_layout=QGridLayout()
        self.title_bar_q_widget_layout.setSpacing(0)
        self.title_bar_q_widget_layout.setContentsMargins(0,0,0,0)
        self.title_bar_q_widget_layout.setVerticalSpacing(0);
        self.title_bar_q_widget_layout.addWidget(self.html_title_bar_webview)
        self.title_bar_q_widget.setLayout(self.title_bar_q_widget_layout)
        #self.html_title_bar_webview.page().mainFrame().evaluateJavaScript("Frame.adjustTitleBarHeight(getComputedStyle(document.body,null).height)")  
        #######
        self.adstar_gui_webview=web_view()
        self.adstar_gui_webview.page().mainFrame().addToJavaScriptWindowObject("Frame",self)          
        self.adstar_gui_webview.show()

        self.adstar_gui_webview.page().mainFrame().setScrollBarPolicy(Qt.Vertical,Qt.ScrollBarAlwaysOff)
        self.adstar_gui_q_widget=QWidget()
        self.adstar_gui_q_widget.setContentsMargins(0,0,0,0)
        self.adstar_gui_q_widget_layout=QGridLayout()
        self.adstar_gui_q_widget_layout.setSpacing(0)
        self.adstar_gui_q_widget_layout.setVerticalSpacing(0)
        self.adstar_gui_q_widget_layout.setContentsMargins(0,0,0,0)
        self.adstar_gui_q_widget_layout.addWidget(self.adstar_gui_webview)
        self.adstar_gui_q_widget.setLayout(self.adstar_gui_q_widget_layout)

        ######
        #self.html_title_bar.show()
#        self.adstar_gui.show()
        self.layout=QGridLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
        self.layout.setVerticalSpacing(0)
        #self.layout.setHorizontalSpacing(0)        
        self.layout.addWidget(self.title_bar_q_widget)   
        self.layout.addWidget(self.adstar_gui_q_widget)
        self.setLayout(self.layout)
        self.setWindowFlags(Qt.FramelessWindowHint)
    @Slot(str)
    def python_evaluate_variable(self,var):
        global stop_session
        if var != "":
            exec(var)                    
    @Slot(str)
    def setWindowTitle(self,win_title_str):
        self.win_title_str=win_title_str
        self.html_title_bar_webview.page().mainFrame().evaluateJavaScript("document.getElementById('window_title').textContent='%s'" % (self.win_title_str,))

    def closeEvent(self,event):
        global main
        #super(Frame,self).closeEvent(event)
        try:
            if main==None:
                return
        except NameError:
            main=None
        if self==main:
            for child_win in all_windows:
                child_win.close()
                #del self
                QCoreApplication.processEvents()

class web_view_with_move(QWebView):
    def __init__(self,qframe):
        super(web_view_with_move,self).__init__()
        self.mouseup=None
        self.mousedown=None
        self.qframe=qframe
        self.settings().setAttribute(QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
    def mouseReleaseEvent(self,event):
        super(web_view_with_move,self).mouseReleaseEvent(event)        
        self.mouseup=True
        self.mousedown=False

    def mousePressEvent(self, event):
        super(web_view_with_move,self).mousePressEvent(event)        
        self.offset = event.pos()
        self.mouseup=False
        self.mousedown=True

    def init_signals(self):
        self.title_bar_signals=title_bar_signals(self.qframe)
        self.page().mainFrame().addToJavaScriptWindowObject("trigger_title_signals",self.title_bar_signals)



    def mouseMoveEvent(self, event):
        super(web_view_with_move,self).mouseMoveEvent(event)         
        if self.mousedown is True:
            x=event.globalX()
            y=event.globalY()
            x_w = self.offset.x()
            y_w = self.offset.y()
            self.qframe.move(x-x_w, y-y_w)


class title_bar_signals(QObject):
    def __init__(self,qframe):
        super(title_bar_signals,self).__init__()
        self.qframe=qframe
    @Slot(str)
    def close(self,tmp):
        self.qframe.close()
        #del self
        QCoreApplication.processEvents()
    @Slot(str)
    def minimize(self,tmp):
        self.qframe.showMinimized()
        #self.qframe.setWindowState(Qt.WindowMinimized)
        QCoreApplication.processEvents()

class title_bar_q(QWidget):
    def __init__(self):
        super(title_bar_q,self).__init__()


    def mouseReleaseEvent(self,event):
        super(title_bar_q,self).mouseReleaseEvent(event)
        self.mouseup=True
        self.mousedown=False



    def mousePressEvent(self, event):
        super(title_bar_q,self).mousePressEvent(event)        
        self.offset = event.pos()
        self.mouseup=False
        self.mousedown=True


    def mouseMoveEvent(self, event):
        super(title_bar_q,self).mouseMoveEvent(event)
        if self.mousedown is True:
            x=event.globalX()
            y=event.globalY()
            x_w = self.offset.x()
            y_w = self.offset.y()
            main.move(x-x_w, y-y_w)

class web_view(QWebView):
    def __init__(self):
        super(web_view,self).__init__()
        self.settings().setAttribute(QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)

def main():
    try:
        Qapp=QApplication(sys.argv)
        ev_runner=QCoreApplication
        main=Frame()
        main.setAttribute(Qt.WA_DeleteOnClose)
        main.adstar_gui_q_widget.setMinimumWidth(850)
        main.adstar_gui_q_widget.setMaximumWidth(850)    
        main.setMinimumHeight(790)
        main.setMaximumHeight(790)
        main.setWindowTitle("Application")
        ev=QCoreApplication
        main.show()
        QCoreApplication.processEvents()
        Qapp.exec_()
    except KeyboardInterrupt:
        pass    
if __name__=="__main__":
    main()

注意:我如何实现标题栏

  • 我在名为^{的类中创建了一个webview框架
    self.html_title_bar_webview=web_view_with_move(self)
    

  • 我执行了一个init_signal()类,它添加了javascript窗口对象
    self.html_title_bar_webview.init_signals()
    

    enter image description here


  • Tags: selfeventtitleinitmaindefhtmlbar