按钮单击后刷新Python Pyqt5 Qlabel

64jmpszr  于 2023-01-24  发布在  Python
关注(0)|答案(1)|浏览(186)

我用PYQT5在Python GUI中写了一个简单的代码,我想在点击正确的按钮(exp."Das"是正确的)时执行,i将自身递增"1",并且"label1"必须显示新的artikel[i+1]值。
我做了递增,但label1无法显示新字符串,它保留旧字符串。当我递增i值时,label1应更新自身
先谢了

import pandas as pd
import xlrd as xl
from pandas import ExcelWriter
from pandas import ExcelFile
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import *
from PyQt5.QtGui import *

DataF=pd.read_excel("/home/emir/Masaüstü/artikel.xlsx")

print("Column headings:")
print(DataF.columns)

for q in DataF.index:
    print(DataF['artikel'][q])

for q in DataF.index:
    print(DataF['Wort'][q])

for q in DataF.index:
    print(DataF['Plural'][q])


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'Deutsch Lern'
        self.left = 10
        self.top = 100
        self.width = 320
        self.height = 200
        self.i=0
        self.a=False

        self.initUI()
   

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('Der', self)
        button.setToolTip('Kontrol Et')
        button.move(20, 160)
        button.clicked.connect(self.on_click)

        button2 = QPushButton('Die', self)
        button2.setToolTip('Kontrol Et')
        button2.move(110, 160)
        button2.clicked.connect(self.on_click2)

        button3 = QPushButton('Das', self)
        button3.setToolTip('Kontrol Et')
        button3.move(200, 160)
        button3.clicked.connect(self.on_click3)

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # Create textbox

        self.label1 = QLabel(DataF['Wort'][self.i], self)

        self.label1.move(130, 80)
        #if (self.a == True):
        #    self.label2 = QLabel(DataF['Wort'][self.i], self)
        #    self.i += 1
        #    self.label2.move(13, 80)
        #   self.show()
        # I tried create new label when a=True.didn't work.

        self.show()


    @pyqtSlot()
    def on_click(self):

     if(DataF['artikel'][self.i]=="der"):
        #print(DataF['Plural'][1])
        print('dogru')
        a=True
        self.i+=1

     else:
         print("yanlış")
        

    def on_click2(self):

     if (DataF['artikel'][self.i] == "die"):

        print('dogru')
        a=True
        self.i+=1

     else:
         print("yanlış")
         
    def on_click3(self):

     if (DataF['artikel'][self.i] == "das"):

        print('dogru')
        a=True
        self.i+=1

        #self.art()
        print(self.i)
     else:
         print("yanlış")
            


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
ajsxfq5m

ajsxfq5m1#

已解决
button3.clicked.connect(self.on_click3)转到def on_click3(self):之下,并转到if (DataF['artikel'][self.i] == "das"):之内。如果条件正确,则应返回此return self.label1.setText(DataF['Wort'][self.i])),并且易于更改label1。

相关问题