2017-04-25 174 views
0

我剛開始用幾周前的編程,使用Python。幾天後,我開始使用一些PyQt教程,現在我陷入了一些困境。PyQt點擊按鈕後顯示文本

所以我有一個菜單欄這個窗口。就像Windows資源管理器一樣,例如「File」和子菜單「Exit」。

我想要的是每個按鈕在窗口中顯示不同的文本。就這樣。 所以現在當我點擊一個按鈕時,它會進入def並在交互式Python shell上打印一些東西。

def button1(self): 
    print('Button clicked.') 

但我想要的是,它顯示在窗口tekst。如果需要的話

import sys 
from PyQt5.QtCore import QCoreApplication, QSize 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
from PyQt5.uic.properties import QtGui 

class window(QMainWindow): 

    def __init__(self): 

     super(window, self).__init__() 
     self.setGeometry(100, 100, 1000, 600) 
     self.setWindowTitle('Andre\'s control center') 
     self.setWindowIcon(QIcon('ik.jpg')) 


     self.label = QLabel('Dit is het control center van Andre 
     Jochemsen',self) 
     self.label.setGeometry(10,10,400,50) 



     extractAction = QAction('&Exit', self) 
     extractAction.setShortcut('Ctrl+Q') 
     extractAction.setStatusTip('Sluit de applicatie') 
     extractAction.triggered.connect(self.close_application) 

     # Intermaris 

     intAlgemeen = QAction('&Algemeen', self) 
     intAlgemeen.triggered.connect(self.intermaris_algemeen) 

     intIncidenten = QAction('&Incidenten', self) 
     intIncidenten.triggered.connect(self.intermaris_algemeen) 

     intWijzigingen = QAction('&Wijzigingen', self) 
     intWijzigingen.triggered.connect(self.intermaris_algemeen) 

     # Balance 

     balAlgemeen = QAction('&Algemeen', self) 
     balAlgemeen.triggered.connect(self.intermaris_algemeen) 

     balIncidenten = QAction('&Incidenten', self) 
     balIncidenten.triggered.connect(self.intermaris_algemeen) 

     balWijzigingen = QAction('&Wijzigingen', self) 
     balWijzigingen.triggered.connect(self.intermaris_algemeen) 

     # Brakel Atmos 

     braAlgemeen = QAction('&Algemeen', self) 
     braAlgemeen.triggered.connect(self.intermaris_algemeen) 

     braIncidenten = QAction('&Incidenten', self) 
     braIncidenten.triggered.connect(self.intermaris_algemeen) 

     braWijzigingen = QAction('&Wijzigingen', self) 
     braWijzigingen.triggered.connect(self.intermaris_algemeen) 

     self.statusBar() 

     mainMenu = self.menuBar() 
     fileMenu2 = self.menuBar() 
     fileMenu3 = self.menuBar() 
     fileMenu4 = self.menuBar() 


     fileMenu = mainMenu.addMenu('&File') 
     fileMenu2 = mainMenu.addMenu('&Balance') 
     fileMenu3 = mainMenu.addMenu('&Brakel Atmos') 
     fileMenu4 = mainMenu.addMenu('&Intermaris') 


     fileMenu.addAction(extractAction) 
     fileMenu2.addAction(balAlgemeen) 
     fileMenu2.addAction(balIncidenten) 
     fileMenu2.addAction(balWijzigingen) 
     fileMenu3.addAction(braAlgemeen) 
     fileMenu3.addAction(braIncidenten) 
     fileMenu3.addAction(braWijzigingen) 
     fileMenu4.addAction(intAlgemeen) 
     fileMenu4.addAction(intIncidenten) 
     fileMenu4.addAction(intWijzigingen) 


     self.show() 


    def close_application(self): 
    # deze method wordt toegepast op de 1e instance, in dit geval self. 
     choice = QMessageBox.question(self, 'message', 
             "Nooo! R u sure?", QMessageBox.Yes | 
             QMessageBox.No, QMessageBox.No) 

     if choice == QMessageBox.Yes: 
      print('quit application') 
      sys.exit() 
     else: 
      pass 

    def intermaris_algemeen(self): 
     print('Button clicked.') 



    def run(): 
    app = QApplication(sys.argv) 
    Gui = window() 
    sys.exit(app.exec_()) 

    run() 

回答

0

更換print('Button clicked.')self.label.setText('Button clicked.') 和點擊後

完整代碼 '按鈕點擊。'文字將會出現,而不是'Dit is het control center van van Andre Jochemsen';