【发布时间】:2021-01-15 19:55:19
【问题描述】:
我最近开始学习 pyQt5,遇到了一个问题,即整个标签没有显示在窗口上。有没有快速解决这个问题的方法?这是我到目前为止的代码,并且还附上了窗口的图像->单击按钮之前:
点击按钮后:
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class MyWindow(QMainWindow):
# Create an innstance of QMainWindow
def __init__(self):
super(MyWindow, self).__init__() # parent constructor
self.setGeometry(400, 200, 1000, 750) # sets size of window
self.setWindowTitle("The birds work for the bourgeoisie") # sets title of window
self.initUI()
def initUI(self):
# stuff we want in window
# Step 1: Define an application
# Labels
self.label = QtWidgets.QLabel(self) # where label is located
self.label.setText("birb wants freedom")
self.label.move(400,200)
# Buttons
self.b1 = QtWidgets.QPushButton(self)
self.b1.setText("Free birb")
self.b1.move(410,230)
# Map button to an event
self.b1.clicked.connect(self.clicked)
# Step 2: Create event for button click
def clicked(self):
self.label.setText("FREEDOM AT LAST!")
def window():
app = QApplication(sys.argv) # passing cmmd line args to QtApp
win = MyWindow() # widget shown in the application
win.show() # brings up window
sys.exit(app.exec_()) # winndow shows up nicely and exits properly
window()
【问题讨论】:
标签: python python-3.x user-interface pyqt5