【问题标题】:Python Pyqt5 Qlabel refreshing after button Click按钮单击后Python Pyqt5 Qlabel刷新
【发布时间】:2018-09-01 05:06:50
【问题描述】:

我使用 PYQT5 在 Python GUI 中编写了一个简单的代码,当我点击一个正确的按钮时我想做(exp。“Das”是正确的)我自己增加“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_())

【问题讨论】:

  • 我没有看到你在任何地方更新label1...?
  • 在 App(QWidget) 类中可以看到: ----- self.label1 = QLabel(DataF['Wort'][self.i], self) ,self.label1.move( 130, 80)
  • 我尝试使用 if cont 进行更新。当按钮变为真值时,我输入了一个布尔值。将捕获布尔值并写入新的“i”值。但没有用。
  • 分享你的 .xlsx
  • 嗨 eyllanesc,我阅读了您之前的帖子 (stackoverflow.com/questions/41819082/updating-pyqt-label?rq=1),它非常有用,但我无法应用我的项目。我附上了 .xlsx 文件 yadi.sk/i/tZza3M-a3aa8sX

标签: python pyqt5


【解决方案1】:

已解决

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 很容易更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多