我想通過單擊 QPushButton 將新的動態創建的小部件直接發送到特定的容器 QWidget 中。我嘗試了一些東西,但無濟于事,它不起作用。
def __init__(self):
QMainWindow.__init__(self)
self.resize(600, 500)
self.wd = wd= QWidget(self)
wd.resize(300,150)
wd.move(10, 100)
bt = QPushButton('Create a button', self)
bt.clicked.connect(self.creat_textLabel)
bt.resize(100,30)
bt.move(0, 0)
def creat_textLabel(self):
lbl = QLabel('user new', self)
lbl.move(0, 0)
lbl.show
uj5u.com熱心網友回復:
(1) - 我添加了一些進口
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
(2) - 也添加了這個,創建并呼叫你的應用程式的主類......
class MainWindow(QMainWindow):
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
(3) - 我重新定位了您的 contenair,使其不會與要在位置 (move(0,0)) 中創建的新標簽混合
wd.move(10, 100)
(4) - 我添加了一些顏色來幫助識別小部件......
wd.setStyleSheet('background:red;')
lbl.setStyleSheet('background:blue; color:white;')
(5節) - 重點在這里
lbl = QLabel('user new', self) #doing this create & positionne QLabel in self == myApp
lbl = QLabel('user new', self.wd) #I suggest this
最后,下面 ==> 完整的固定作業代碼!
#!/usr/bin/env python
#coding: utf-8
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(600, 500)
self.wd = wd= QWidget(self)
wd.setStyleSheet('background:red;')
wd.resize(300,150)
wd.move(10, 100)
bt = QPushButton('Create a button', self)
bt.clicked.connect(self.creat_textLabel)
bt.resize(100,30)
bt.move(100, 250)
def creat_textLabel(self):
lbl = QLabel('user new', self.wd)
lbl.setStyleSheet('background:blue; color:white;')
lbl.move(0, 0)
lbl.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )

而且我建議你改變誠聘英才喜歡的東西:
Pyqt5:將動態創建的 QWidget (QLbe) 托管到特定的容器 (QWidget) 中
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/409363.html
標籤:
上一篇:這段C 代碼的哪一部分是父視窗?
下一篇:禁用ReactJS中的按鈕
