Link 到 运行 .py 文件的按钮
Link a pushbutton to run a .py file
我想要做的是能够通过单击 main.py 上的按钮打开 grid.py。最好保持相同的结构(ui 文件以及主文件和网格文件 python 文件)。谢谢,
所以,我创建了一个简单的 UI 界面,其中仅包含一个带有 Qt Designer 的按钮 (test-open.ui):
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>604</width>
<height>424</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>230</x>
<y>160</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>604</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
然后我创建了主要的 .py 文件 (main.py):
from PyQt5 import QtWidgets, uic
app = QtWidgets.QApplication([])
dlg = uic.loadUi(r"E:\Python\Project\test-open.ui")
dlg.show()
app.exec()
最后,这是我用 WxPython 创建的网格,我想通过单击按钮打开它 (grid.py)
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 table - pythonspot.com'
self.left = 0
self.top = 0
self.width = 460
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add box layout, add table to box layout and add box layout to widget
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
# Show widget
self.show()
def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(4)
self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.move(0,0)
# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)
@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
你混淆了概念,程序不是一组文件,而是基于classes 创建的对象之间的交互。因此,在执行任何操作之前,我假设文件具有以下结构:
├── grid.py
├── main.py
└── test-open.ui
另一方面,在 App class 构造函数中,您显示的是 window 而这不是您想要的,最初 window 必须关闭,所以我将消除它线。进入正题,必须在main中导入grid模块才能创建App widget对象
grid.py
import sys
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QWidget,
QAction,
QTableWidget,
QTableWidgetItem,
QVBoxLayout,
)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = "PyQt5 table - pythonspot.com"
self.left = 0
self.top = 0
self.width = 460
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add box layout, add table to box layout and add box layout to widget
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(4)
self.tableWidget.setItem(0, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.move(0, 0)
# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)
@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(
currentQTableWidgetItem.row(),
currentQTableWidgetItem.column(),
currentQTableWidgetItem.text(),
)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
main.py
from PyQt5 import QtWidgets, uic
from grid import App
if __name__ == "__main__":
import os
import sys
app = QtWidgets.QApplication(sys.argv)
file = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test-open.ui"
)
dlg = uic.loadUi(file)
w = App()
dlg.pushButton.clicked.connect(w.show)
dlg.show()
sys.exit(app.exec_())
我想要做的是能够通过单击 main.py 上的按钮打开 grid.py。最好保持相同的结构(ui 文件以及主文件和网格文件 python 文件)。谢谢,
所以,我创建了一个简单的 UI 界面,其中仅包含一个带有 Qt Designer 的按钮 (test-open.ui):
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>604</width>
<height>424</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>230</x>
<y>160</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>604</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
然后我创建了主要的 .py 文件 (main.py):
from PyQt5 import QtWidgets, uic
app = QtWidgets.QApplication([])
dlg = uic.loadUi(r"E:\Python\Project\test-open.ui")
dlg.show()
app.exec()
最后,这是我用 WxPython 创建的网格,我想通过单击按钮打开它 (grid.py)
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget,QTableWidgetItem,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 table - pythonspot.com'
self.left = 0
self.top = 0
self.width = 460
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add box layout, add table to box layout and add box layout to widget
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
# Show widget
self.show()
def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(4)
self.tableWidget.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3,2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3,3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3,4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.move(0,0)
# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)
@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
你混淆了概念,程序不是一组文件,而是基于classes 创建的对象之间的交互。因此,在执行任何操作之前,我假设文件具有以下结构:
├── grid.py
├── main.py
└── test-open.ui
另一方面,在 App class 构造函数中,您显示的是 window 而这不是您想要的,最初 window 必须关闭,所以我将消除它线。进入正题,必须在main中导入grid模块才能创建App widget对象
grid.py
import sys
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QWidget,
QAction,
QTableWidget,
QTableWidgetItem,
QVBoxLayout,
)
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QWidget):
def __init__(self):
super().__init__()
self.title = "PyQt5 table - pythonspot.com"
self.left = 0
self.top = 0
self.width = 460
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add box layout, add table to box layout and add box layout to widget
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(4)
self.tableWidget.setItem(0, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(0, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(0, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(1, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(1, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(2, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(2, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 0, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 1, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3, 2, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.setItem(3, 3, QTableWidgetItem("Cell (1,2)"))
self.tableWidget.setItem(3, 4, QTableWidgetItem("Cell (1,1)"))
self.tableWidget.move(0, 0)
# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)
@pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(
currentQTableWidgetItem.row(),
currentQTableWidgetItem.column(),
currentQTableWidgetItem.text(),
)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
main.py
from PyQt5 import QtWidgets, uic
from grid import App
if __name__ == "__main__":
import os
import sys
app = QtWidgets.QApplication(sys.argv)
file = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "test-open.ui"
)
dlg = uic.loadUi(file)
w = App()
dlg.pushButton.clicked.connect(w.show)
dlg.show()
sys.exit(app.exec_())