Home > Net >  PyQt5 Items of my Gui does not appear when I use the loop "while" to check if a checkbox i
PyQt5 Items of my Gui does not appear when I use the loop "while" to check if a checkbox i

Time:07-09

I'm coding a program that converts a currency into the other currency you choosed from the ListBox.

I'm having trouble with this program. If I use the loop while to check, items of my Gui does not appear to me.

Here are my codes:

from selenium import webdriver
from selenium.webdriver.common.by import By as by
from selenium.webdriver.common.keys import Keys as key
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from interface import *
import time
import sys

class CurrencyConverter(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Interface()
        self.ui.setupUi(self)
        self.show()
        self.ui.amountbox.setVisible(1)
        self.stepOne()

    def stepOne(self):
        while True:
            if self.ui.Amount.isChecked():
                self.ui.amountbox.setVisible(0)
            else:
                self.ui.amountbox.setVisible(1)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWindow = CurrencyConverter()
    app.exec_()

CodePudding user response:

Here is an example of what is suggested in the comments.

You connect with the buttons toggled signal and use your setOne method as the slot. The signal is emited with a boolean value as an argument indicating the checkstate which you can then use to conditionally perform some action. The signal is automatically emitted every time the state of the button is changed, so no while loops are necessary.

...

class CurrencyConverter(QMainWindow):
    def __init__(self):
        super().__init__()
        ...
        ...
 
        self.ui.amount.toggled.connect(self.stepOne)  # signal connection

    def setOne(self, ischecked):   # slot method
        if ischecked:    
            self.ui.amountbox.setVisible(0)
        else:
            self.ui.amountbox.setVisible(1)
  • Related