Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - How can I change the text of the edit line and add and subtract it?

I have two edit lines that when a letter from a dictionary is typed in it, it returns a number, and this number is added to the next edit line number and printed on a label. Now I want the previous number to be reduced and the new number to be added when I change one of the edit lines.

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QLabel
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
oroh, _ = loadUiType("oroh.ui")

class OrOh(QWidget, oroh):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)
    
        self.lin_sh1.returnPressed.connect(self.shift1)
        self.lin_sh2.returnPressed.connect(self.shift2)
         
    def shift1(self):
        shifts = {"m":7, "s":7, "v":7, "t":7, "e":3, "le":5, "ld":12,
                  "n":12, "ln":14, "mn":19, "en":17, "me":10, "f":24}
    
        sh1 = self.lin_sh1.text()
        if sh1 in shifts.keys():
           a = shifts[sh1]
           print(a)
        else:
           a = 0
           print(a)
        self.lbl_shifts.setText(f"{a}")
    
    def shift2(self):
        shifts = {"m":7, "s":7, "v":7, "t":7, "e":3, "le":5, "ld":12,
                  "n":12, "ln":14, "mn":19, "en":17, "me":10, "f":24}
        a = self.lbl_shifts.text()
    
        sh2 = self.lin_sh2.text()
        if sh2 in shifts.keys():
            b = shifts[sh2] 
        else:
            b = 0
        
        result = int(a) + int(b)
        self.lbl_shifts.setText(f"{result}")
    
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = OrOh()
    window.show()
    app.exec_()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since the keys can use more than one character, a possible solution is to use the textChanged(text) signal and call the function to compute the shifts afterwards.

The fact, though, is that the two existing functions are actually flawed: they almost do the same thing, but don't consider the two fields as they should.

I suggest you to merge those functions and use the textChanged to "reset" the value only when a valid key is entered. Note that this implies a logic that is not immediate to understand (even for the user) and might also cause some confusion

class OrOh(QWidget, oroh):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)

        self.shifts = {"m":7, "s":7, "v":7, "t":7, "e":3, "le":5, "ld":12,
                  "n":12, "ln":14, "mn":19, "en":17, "me":10, "f":24}
    
        self.lin_sh1.returnPressed.connect(self.computeShift)
        self.lin_sh2.returnPressed.connect(self.computeShift)

        self.lin_sh1.textChanged.connect(self.checkText)
        self.lin_sh2.textChanged.connect(self.checkText)

    def checkText(self):
        lin1 = self.lin_sh1.text()
        lin2 = self.lin_sh2.text()
        if self.lbl_shifts.text() and (not lin1 or not lin2):
            self.computeShift()

    def computeShift(self):
        lin1 = self.lin_sh1.text()
        lin2 = self.lin_sh2.text()

        if ((lin1 in self.shifts and not lin2) or 
            (lin2 in self.shifts and not lin1) or
            (lin1 in self.shifts and lin2 in self.shifts)):
                shift = self.shifts.get(lin1, 0) + self.shifts.get(lin2, 0)
                self.lbl_shifts.setText(str(shift))

I would suggest you to add some validation logic instead, or at least always use the textChanged signal. A simple solution would be to change the text color whenever the text entered is not in the dictionary, and eventually clear it when the focus changes:

class OrOh(QWidget, oroh):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)

        self.shifts = {"m":7, "s":7, "v":7, "t":7, "e":3, "le":5, "ld":12,
                  "n":12, "ln":14, "mn":19, "en":17, "me":10, "f":24}
    
        self.lin_sh1.textChanged.connect(self.computeShift)
        self.lin_sh2.textChanged.connect(self.computeShift)

        self.lin_sh1.editingFinished.connect(lambda: self.checkField(self.lin_sh1))
        self.lin_sh2.editingFinished.connect(lambda: self.checkField(self.lin_sh2))

    def checkField(self, field):
        if not field.text() in self.shifts:
            field.setText('')

    def computeShift(self):
        shift = 0
        
        for field in (self.lin_sh1, self.lin_sh2):
            value = self.shifts.get(field.text(), 0)
            if field.text() and not value:
                field.setStyleSheet('color: red;')
            else:
                field.setStyleSheet('')
            shift += self.shifts.get(field.text(), 0)
        self.lbl_shifts.setText(str(shift))

Another possibility would be to use an editable QComboBox (with the insert policy set to NoInsert) and check the shift only when the inserted text is in the list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...