本文目錄一覽:
- 1、QT中調用Python
- 2、在qt程序中怎麼運行一個python程序
- 3、如何嵌入在一個PyQt的窗口小部件一個Python解釋器
- 4、Qt中怎麼可以使用python嗎
- 5、Qt下無法調用python,打不開文件為什麼
- 6、我使用QT做好了界面,用python寫好了功能,怎麼把兩者連接起來呢?
QT中調用Python
如果你是嵌入的話,應該沒問題。python的庫會暴露c api的,你的qt程序照著python文檔中的embedded章節就好。 看樣子,你的是linux下的,不過一般頭文件就是include/python.h,怎麼會是include/python2.7呢?你寫錯了吧。庫文件應該是在lib下面。
在qt程序中怎麼運行一個python程序
因為process.start()函數實質是採用命令行中”start XXX”的方式啟動XXX。這裡的XXX僅僅適用於大部分的exe可執行文件以及一些常用文件。對於py或者pyw文件都不行。
有兩種解決方案:
另外編寫一個bat用來啟動py文件,就可以使用start啟動這個bat就可以了;
使用另一個函數「QDesktopServices::openUrl」來啟動py文件。具體使用方法請自行百度。不做過多說明。
如何嵌入在一個PyQt的窗口小部件一個Python解釋器
”’
Created on 18-03-2012
@author: Paweł Jarosz
”’
import os, sys
import atexit
from PySide import QtCore, QtGui
from IPython.zmq.ipkernel import IPKernelApp
from IPython.lib.kernel import find_connection_file, connect_qtconsole
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.config.application import catch_config_error
class IPythonLocalKernelApp(IPKernelApp):
“””IPython kernel application with nonblocking loop, running in dedicated thread.
example:
app = QtGui.QApplication([])
kernelapp = IPythonLocalKernelApp.instance()
kernelapp.start()
namespace = kernelapp.get_user_namespace()
namespace[“QtGui”]=QtGui
namespace[“QtCore”]=QtCore
app.exec_()”””
#DEFAULT_INSTANCE_ARGS starting commandline
DEFAULT_INSTANCE_ARGS = [‘qtconsole’,’–pylab=inline’, ‘–colors=linux’]
@catch_config_error
def initialize(self, argv=None):
super(IPythonLocalKernelApp, self).initialize(argv)
self.kernel.eventloop = self.loop_qt4_nonblocking
def loop_qt4_nonblocking(self, kernel):
“””Non-blocking version of the ipython qt4 kernel loop”””
kernel.timer = QtCore.QTimer()
kernel.timer.timeout.connect(kernel.do_one_iteration)
kernel.timer.start(1000*kernel._poll_interval)
def start(self, argv=DEFAULT_INSTANCE_ARGS):
“””Starts IPython kernel app
argv: arguments passed to kernel
“””
self.initialize(argv)
self.heartbeat.start()
if self.poller is not None:
self.poller.start()
self.kernel.start()
def get_connection_file(self):
“””Returne current kernel connection file.”””
return self.connection_file
def get_user_namespace(self):
“””Returns current kernel userspace dict”””
return self.kernel.shell.user_ns
class IPythonConsoleQtWidget(RichIPythonWidget):
“””Ipython console Qt4+ widget
Usage example:
app = QtGui.QApplication([])
kernelapp = IPythonLocalKernelApp.instance()
kernelapp.start()
namespace = kernelapp.get_user_namespace()
widget = IPythonConsoleQtWidget()
widget.set_default_style(colors=’linux’)
widget.connect_kernel(connection_file=kernelapp.get_connection_file())
# if you won’t to connect to remote kernel:
widget.connect_kernel(connection_file=’kernel-16098.json’)
widget.show()
namespace[“widget”] = widget
namespace[“QtGui”]=QtGui
namespace[“QtCore”]=QtCore
app.exec_()”””
_connection_file = None
def __init__(self, *args, **kw):
RichIPythonWidget.__init__(self, *args, **kw)
self._existing = True
self._may_close = False
self._confirm_exit = False
def _init_kernel_manager(self):
km = QtKernelManager(connection_file=self._connection_file, config=self.config)
km.load_connection_file()
km.start_channels(hb=self._heartbeat)
self.kernel_manager = km
atexit.register(self.kernel_manager.cleanup_connection_file)
def connect_kernel(self, connection_file, heartbeat=False):
“””Connect’s to ipython kernel.
connection_file – connection file to use
heartbeat – should start heartbeat server? Workaround for problems with inproc embedded kernels
(right click save image as/save as html kills kernel heartbeat/pool(??) serwer “””
self._heartbeat = heartbeat
if os.path.exists(connection_file):
self._connection_file = connection_file
else:
self._connection_file = find_connection_file(connection_file)
self._init_kernel_manager()
app = QtGui.QApplication([])
kernelapp = IPythonLocalKernelApp.instance()
kernelapp.start()
namespace = kernelapp.get_user_namespace()
widget = IPythonConsoleQtWidget()
widget.set_default_style(colors=’linux’)
widget.connect_kernel(connection_file=kernelapp.get_connection_file())
# if you won’t to connect to remote kernel:
# widget.connect_kernel(connection_file=’kernel-16098.json’)
widget.show()
namespace[“widget”] = widget
namespace[“QtGui”]=QtGui
namespace[“QtCore”]=QtCore
app.exec_()
IPython的一個小部件。
首先,啟動一個內核kernelapp = IPythonLocalKernelApp.instance()
kernelapp.start()
接下來,創建並連接IPython的控制台小工具widget = IPythonConsoleQtWidget()
widget.set_default_style(colors=’linux’)
widget.connect_kernel(connection_file=kernelapp.get_connection_file())
# if you won’t to connect to remote kernel:
# widget.connect_kernel(connection_file=’kernel-16098.json’)
widget.show()
代碼是基於IPython的0.12穩定。
有與右擊另存為HTML問題
Qt中怎麼可以使用python嗎
這個看你要求了,如果只是單純調用,和Qt沒什麼關係,直接看python手冊中C、C++調用python相關的內容就行了。
如果你的python代碼中還需要操作Qt相關的東西,那麼你需要的 PythonQt 這個第三方的模塊(概念上類似於Qt自己的QtScript模塊)
Qt下無法調用python,打不開文件為什麼
#include QCoreApplication
//包含調用Python相應的頭文件
#include Python.h
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//初始化Python解釋器,這是調用操作的第一步
Py_Initialize();
if( !Py_IsInitialized() ){
return -1;
}
//執行單句Python語句,用於給出調用模塊的路徑,否則將無法找到相應的調用模塊
PyRun_SimpleString(“import sys”);
PyRun_SimpleString(“sys.path.append(‘./’)”);
//獲取qt_python_fun.py模塊的指針
PyObject* pModule = PyImport_ImportModule(“qt_python_fun”);
if (! pModule){
printf(“Can’t open python file\n”);
return -1;
}
//獲取hello函數的指針
PyObject* pFunhello = PyObject_GetAttrString(pModule,”hello”);
if (!pFunhello){
printf(“Get function hello failed\n”);
return -1;
}
//調用函數,傳入參數為NULL
PyObject_CallFunction(pFunhello,NULL);
//銷毀Python解釋器,這是調用的最後一步
Py_Finalize();
return a.exec();
}
作者:sleepyjoker
鏈接:
來源:簡書
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
我使用QT做好了界面,用python寫好了功能,怎麼把兩者連接起來呢?
答:我知道目前你應該就是用qt designer設計好了界面,然後你的Python代碼是PyCharm,那麼要將兩者連接起來的話,有兩種方法。
使用pyuic功能,這個功能需要你在PyCharm上面配置一下pyqt,可以將Qt的UI文件轉化為一個Python的類,然後你就可以直接在你的工程裡面引用這個類;
無需將UI文件轉化為Python中的類,而只需要直接載入該UI文件就可以了,使用如下代碼,如圖紅框所示,其中涉及到PyQt.uic的loadUi類。
直接載入UI文件
希望對你有幫助~
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/278941.html