我尝试运行我的代码,它可以正常运行,但未按预期运行。我实际上希望按钮显示在我显示的窗口中
我已经从PyQt5.QtGui导入了QPushButton并编写了所需的代码
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QToolTip, QPushButton)
from PyQt5.QtGui import (QIcon, QFont)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QWidget()
w.resize(500, 500)
w.move(700, 50) #I can use w.setGeometry(v,w,x,y) here too
w.setWindowTitle('DIDOs APP')
w.setWindowIcon(QIcon('C:\\Users\\Pictures\\DB\default.jpg'))
class Example(QWidget):
def __init__self(w):
super().__init__()
w.initUI()
def initUI(w):
QToolTip.setFont(QFont('SansSerif', 10))
w.setToolTip('This is a <b>QWidget</b> widget')
btn = QPushButton('Button', w)
btn.setToolTip('This is a <b>QWidget</b> widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)
btn.show(w)
w.show()
sys.exit(app.exec_())
我希望在运行代码时,按钮小部件应该显示在窗口中
答案 0 :(得分:0)
尝试一下:
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QToolTip, QPushButton)
from PyQt5.QtGui import (QIcon, QFont)
class Example(QWidget):
# def __init__self(self): # ---
def __init__(self): # +++
super().__init__()
self.initUI()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn = QPushButton('Button', self)
btn.setToolTip('This is a <b>Button</b> widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)
# btn.show(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Example() # - QWidget()
w.resize(500, 500)
w.move(700, 50) #I can use w.setGeometry(v,w,x,y) here too
w.setWindowTitle('DIDOs APP')
w.setWindowIcon(QIcon('D:/_Qt/img/pyqt.jpg'))
w.show()
sys.exit(app.exec_())