Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
showing a value in the GUI
i made an GUI and try to show a number that is updating irregular.
but the value is not changing in the GUI.
Denni 0 last edited by
@immmomm supply some minimal functional representation of your current issue and perhaps we can help solve your issue without it we are just shooting in the dark (aka taking wild guesses)
okay so I have this function in another .py file:
def balance(): balfunc = list(con.get_accounts().get('balance')) bal = str((balfunc)) print(bal) return(bal)
the output of this is: 49970.07
the output can change.
and this is the main file for the GUI
import sys from PyQt5 import QtWidgets, QtGui, QtCore from multiprocessing import freeze_support from odin.data.basic import balance # Import main page ui file from odin.gui.main_window import Ui_MainWindow # Import dialog page ui file #from odin.gui import dialogue_page class MainWindow_exec(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self, parent=None): QtWidgets.QMainWindow.__init__(self, parent) self.setupUi(self) # Functions in the main_window self.pushButton_quit.clicked.connect(self.close) self.label_show_cash.setText(str(balance())) if __name__ == '__main__': # This line help avoid errors related to multiprocessing when freezing the application. freeze_support() # Start application app = QtWidgets.QApplication(sys.argv) app.setQuitOnLastWindowClosed(False) # Set application style. Styles: WindowsVista,Windows,Fusion # s = QtWidgets.QStyleFactory.create('Fusion') # app.setStyle(s) # Set application id and icon for taskbar myappid = 'odin' # arbitrary string app_icon = QtGui.QIcon() app_icon.addFile('basic_logo.png', QtCore.QSize(16, 16)) app_icon.addFile('basic_logo.png', QtCore.QSize(24, 24)) app_icon.addFile('basic_logo.png', QtCore.QSize(32, 32)) app_icon.addFile('basic_logo.png', QtCore.QSize(48, 48)) app_icon.addFile('basic_logo.png', QtCore.QSize(256, 256)) app.setWindowIcon(app_icon) # Call mainwindow and start eventloop MainWindow1 = MainWindow_exec() MainWindow1.show() sys.exit(app.exec_())
i want to see the value at the balance label I made.
@Denni-0 done :)
Denni 0 last edited by Denni 0
Okay well that example is not fully functional because you are importing Ui_MainWindow and that is not being provided as well as a few other issues -- further it would not nave been the minimal version if you included your entire Ui_Mainwindow.
Now I am assuming you might be using the Designer or something like it to create that UI which is (with pyqt5) totally unnecessary and problematic as you move forward. Note I understand some folks put a lot of work into the Designer and it might be nice for some down and dirty things but it really really is not necessary for one can just as easily and more cleanly create the UI using straight pyqt5 without the use of the horribly inefficient code garbage producing Designer. The added benefit is that the code (if done right) is clean easy to understand and efficient and easily manipulated later to do other things because you now have a much better understanding of what it is doing and why. To that extent here might be a rendition of what you are doing rendering in pure pyqt5 format
# First let us only import what we need # I choose to convert this to sysExit to help clarify what the exit command is and yet still import only what I need from sys import exit as sysExit from multiprocessing import freeze_support from PyQt5.QtCore import Qt #PyQt Containers -- yeah Containers are Objects but I think you understand what I mean from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QVBoxLayout, QHBoxLayout, QSplitter #PyQt Objects from PyQt5.QtWidgets import QPushButton, QLabel, QTextEdit class ItemDsplyr(QWidget): def __init__(self, parent): QWidget.__init__(self) # Again just keeping track of the parent to this self.CntrPane = parent # Note I do not always do this just showing it here as an example self.MainWin = parent.MainWin self.btnExit = QPushButton('Exit') self.btnExit.clicked.connect(parent.close) self.btnShow = QPushButton('Show') self.btnShow.clicked.connect(parent.ShowBalance) self.btnZero = QPushButton('Zero') self.btnZero.clicked.connect(parent.ZeroBalance) self.lblShowCash = QLabel('$0') self.VBox = QVBoxLayout() self.VBox.addWidget(self.btnExit) self.VBox.addWidget(self.btnShow) self.VBox.addWidget(self.btnZero) self.VBox.addWidget(self.lblShowCash) self.VBox.addStretch(1) self.setLayout(self.VBox) @property def CntrPane(self): return self.__parent @CntrPane.setter def CntrPane(self, value): self.__parent = value class CenterPane(QWidget): def __init__(self, parent): QWidget.__init__(self) # Keeping track of the parent is sometimes necessary # and sometimes knowing who your grandparent was is # very useful as directly referring to them is easier than # walking up a child - parent tree self.MainWin = parent # Creating a handle to a Child self.ItmDsplyr = ItemDsplyr(self) CntrPane = QSplitter(Qt.Horizontal, self) CntrPane.addWidget(self.ItmDsplyr) CntrPane.addWidget(QTextEdit()) CntrPane.setSizes([75,200]) HBox = QHBoxLayout(self) HBox.addWidget(CntrPane) self.setLayout(HBox) @property def MainWin(self): return self.__parent @MainWin.setter def MainWin(self, value): self.__parent = value def ShowBalance(self): Balance = self.GetBalance() self.ItmDsplyr.lblShowCash.setText(Balance) def ZeroBalance(self): self.ItmDsplyr.lblShowCash.setText('$0.00') def GetBalance(self): # These meant nothing to us since they did not access anything # just showing them here so you understand what GetBalance would # be doing so-to-speak # balfunc = list(con.get_accounts().get('balance')) # bal = str((balfunc)) bal = '$1924.56' return(bal) class MainWindow(QMainWindow): def __init__(self, parent, Cnt): QMainWindow.__init__(self, parent) title = "Main Window " + str(Cnt) left=100;top=100;width=300;height=200 self.setWindowTitle(title) self.setGeometry(left,top,width,height) self.MainApp = parent # Did not create a handle to the Child here but could have if needed self.setCentralWidget(CenterPane(self)) @property def MainApp(self): return self.__parent @MainApp.setter def MainApp(self, value): self.__parent = value class MainProcess(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) # Create and Call MainWindow 1 MainWindow1 = MainWindow(self, 1) MainWindow1.show() # Assuming you want more Windows they would be # created/called from here as this is now your # primary parent for everything of course some # code will need to be added to facilitate this if __name__ == '__main__': # This line help avoid errors related to multiprocessing when freezing the application. freeze_support() myappid = 'odin' # arbitrary string # I call this the Main Thread because that is what it represents # Note if you not going to use sys.argv then do not include it MainThred = QApplication() # I do not think this is needed now ?? # MainThred.setQuitOnLastWindowClosed(False) # If an icon had been included which is was not # app_icon = QtGui.QIcon() # ... # app.setWindowIcon(app_icon) # Start application -- Note the bare minimum should be handled from within here MainProcesser = MainProcess() # Start main thread eventloop sysExit(MainThred.exec_())
Let me know if this helps you with your problem or not and of if you have any issues following along with what I have rendered here. Also if you have not looked into them yet Stackable Widgets can be fairly handy for something like this as well.
JonB last edited by JonB
but the value is not changing in the GUI.
Do you understand that you need to hook onto an "event" which arrives when the value changes, and update the label in response to that? In Qt you need to write a slot in your code and connect it to a signal which arrives when the value has, or might have, changed. The signal might be a dedicated one which is emitted when the balance changes, or it might be for a
QTimerwhich is ticking away and calls for update periodically.
Denni 0 last edited by
Yes as @JonB points out the value only changes based on whatever event you tie it to that triggers that value change. A static label is just that static on its own it just simply holds a value until some external event changes that value such as the button click events I added to the short example I gave but yes you could tie that event to a timer, to a database change, to just about anything that would trigger something and then use that triggered event to update your the label but in every case you have to purposefully and directly change the contents of that label -- even in software that supports linking a field in the GUI directly to a field in a database (a bad thing to to most of the time btw) the update event that updates the database is seamlessly used to update the field within the GUI. So think of it more as cause and effect -- the effect you are looking for is your field to update the amount -- but what you are missing is the cause -- what causes this update to occur?
thank you all!!
so the ui-designer is for "nothing"?
I use it because i can change the design fast without loosing the code for the action for buttons
Denni 0 last edited by Denni 0
No I would say the UI-Designer is for creating down and dirty proto-types of things that you do not plan to hold on to or work with beyond using it to show off a concept. However it is also used by folks that do not care about garbage code and/or by simply lazy programmers or folks intimidated by the concept of learning how to do it right or simply folks not in the know. So now you know ;)
Now if you learn the actual functionality of pyqt5 and python -- and learn how to properly Class-ify it -- then changing (or not changing) any aspect is as simple or even simpler than using the Designer. Yes it might seem a bit daunting at first and frankly what the Designer spews out makes it even scarier than it is because as I have stated -- it produces some of the ugliest code that I have seen in a long while but then clean efficient code was not what it was designed for I guess.
I however have posted a couple of quality basic examples here in this forum that you can use to get a better and perhaps quicker handle on getting away from the Designer and creating your own code that is easier to work with in the long run.