我构建了一个运行几千行代码的应用程序。
我试图通过将 python 代码编译为 c 代码,然后编译为目标文件来隐藏代码。
该应用程序在 pyside 中运行良好。我转换了这个 python 文件。py
to a .c
文件并将 c 编译为目标文件.o
。该文件的名称是test2.py
以及我将其转换为.o
file:
cython --embed -o test2.c test2.py
从上面我现在将 test2.c 编译为 test2.o 如下
gcc -I /usr/include/python2.7 -o test2.o -fPIC test2.c -lpython2.7 -lpthread -lm -lutil -ldl
我的问题是 webkit 框架中设计的最小化和关闭按钮在编译后不起作用(但是从 python 文件执行时效果很好)并且其他 addtoJavascripWindowObjects 不起作用
我使用 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 框架Frame
self.html_title_bar_webview=web_view_with_move(self)
我执行了一个 init_signal() 类,它添加了 javascript 窗口对象
self.html_title_bar_webview.init_signals()