2017-04-15 116 views
0

我試圖顯示帶有滾動條的圖像標籤在一個框佈局。 但是,滾動區域顯示錯誤的大小錯誤的地方。 你能告訴我我做錯了什麼嗎?PyQt5:QScrollArea不附加到標籤

import sys 
from PyQt5 import QtCore 
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QLabel, QScrollArea 
from PyQt5.QtGui import QPixmap 


class ApplicationWindow(QMainWindow): 
    def __init__(self): 
     QMainWindow.__init__(self) 

     main_widget = QWidget(self) 

     btn = QPushButton("Bye", self) 
     btn.clicked.connect(self.close) 

     img = QPixmap("1.jpg") 
     label = QLabel(main_widget) 
     label.setPixmap(img) 

     scrollArea = QScrollArea(main_widget) 
     scrollArea.setWidgetResizable(True) 
     scrollArea.setWidget(label) 

     l = QVBoxLayout(main_widget) 
     l.addWidget(label) 
     l.addWidget(btn) 

     self.setCentralWidget(main_widget) 


    def closeEvent(self, ce): 
     self.close() 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    aw = ApplicationWindow() 
    aw.show() 
    app.exec_() 

結果是:

screenshot

回答

0

的問題是,代替將QLabelQVBoxLayout您必須添加QScrollArea。您必須更改:

l.addWidget(label) 

l.addWidget(scrollArea) 

enter image description here