QtDesigner 与 PyQt 的结合(更具体地说,将按钮添加到在 QtDesigner 中创建的选项卡
QtDesigner in combination with PyQt (more specific adding a button to a tab created in QtDesigner
我是 PyQt 和 QtDesigner 的新手,所以我想做的事情可能很简单,但我在任何地方都找不到工作示例。
我在 QtDesigner 中创建了一个 GUI,其中包含一个 tabWidget 和多个名为 tab 的选项卡(即 QWidgets),tab_2 等。
现在我正在尝试向第一个选项卡(称为选项卡)添加一个按钮,例如。我之前的尝试在新 window 中创建了按钮。
正确的做法是什么?
import sys
from PyQt5 import QtWidgets,QtCore, QtGui,uic
from PyQt5.Qt import QPushButton
class Main(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = uic.loadUi('example.ui',self)
self.ui.tab.btn1=QtWidgets.QPushButton('buttonn')
self.ui.tab.btn1.show()
if __name__ == '__main__':
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
window=Main()
window.show()
sys.exit(app.exec_())
The normal way to use QTabWidget is to do the following: Create a
QTabWidget. Create a QWidget for each of the pages in the tab dialog,
but do not specify parent widgets for them. Insert child widgets into
the page widget, using layouts to position them as normal. Call
addTab() or insertTab() to put the page widgets into the tab widget,
giving each tab a suitable label with an optional keyboard shortcut.
试试这个:
class Main(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = uic.loadUi('example.ui',self)
btn1=QtWidgets.QPushButton('buttonn')
self.ui.tabwidget.addTab(btn1, 'Tab name')
如果您已经创建了选项卡,只需将按钮创建为选项卡内小部件的子项即可:
btn1=QtWidgets.QPushButton('buttonn', self.ui.tab)
我是 PyQt 和 QtDesigner 的新手,所以我想做的事情可能很简单,但我在任何地方都找不到工作示例。
我在 QtDesigner 中创建了一个 GUI,其中包含一个 tabWidget 和多个名为 tab 的选项卡(即 QWidgets),tab_2 等。 现在我正在尝试向第一个选项卡(称为选项卡)添加一个按钮,例如。我之前的尝试在新 window 中创建了按钮。 正确的做法是什么?
import sys
from PyQt5 import QtWidgets,QtCore, QtGui,uic
from PyQt5.Qt import QPushButton
class Main(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = uic.loadUi('example.ui',self)
self.ui.tab.btn1=QtWidgets.QPushButton('buttonn')
self.ui.tab.btn1.show()
if __name__ == '__main__':
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
window=Main()
window.show()
sys.exit(app.exec_())
The normal way to use QTabWidget is to do the following: Create a QTabWidget. Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them. Insert child widgets into the page widget, using layouts to position them as normal. Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.
试试这个:
class Main(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.ui = uic.loadUi('example.ui',self)
btn1=QtWidgets.QPushButton('buttonn')
self.ui.tabwidget.addTab(btn1, 'Tab name')
如果您已经创建了选项卡,只需将按钮创建为选项卡内小部件的子项即可:
btn1=QtWidgets.QPushButton('buttonn', self.ui.tab)