Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:58:18+00:00 2026-06-18T04:58:18+00:00

I’m trying to find a way to update labels which are within dynamically created

  • 0

I’m trying to find a way to update labels which are within dynamically created widgets (that can be deleted), according to properties dynamically set in the preceding widgets.

Is there a way to automatically and dynamically link a pyqt object to a property of other widgets, so that when I change a value anywhere, that object updates itself?

Example: object ‘a’ has property start, bars and end; bars is given, start is taken by the previous object (or 1 if None), end is calculated. object ‘b’ takes its start from a.end and so on.

class myclass(object):
  def __init__(self, referrer=None, value=1):
    self.referrer=referrer
    self.value=value

  def _get_start(self):
    if not self.referrer:
      return 1
    else:
      return self.referrer.end+1

  def _get_end(self):
    return self.start+self.value-1

  start=property(_get_start)
  end=property(_get_end)

def create(value=1):
  if not vList:
    ref=None
  else:
    ref=vList[-1]
  val=myclass(ref, value)
  vList.append(val)

def showList():
  for i in vList:
    print 'item: %d\tstart: %d\tend: %d' % (vList.index(i),i.start, i.end)

vList=[]

If I call create() 3 times, showList() will show:

item: 0 start: 1        end: 1
item: 1 start: 2        end: 2
item: 2 start: 3        end: 3

if I change vList[0].value to 3:

item: 0 start: 1        end: 3
item: 1 start: 4        end: 4
item: 2 start: 5        end: 5

The problem raises when I need to keep those values updated in the gui (think to it as an interface like this): every horizontal widget has a label showing the property of start, a spinbox for bars, and a label for end, and as soon as any spinbox value changes, every subsequent widget should update its start and end properties according to its previous widget and show them in the relative labels.
Moreover, when any widget is deleted, all the subsequent widget should recompute every property.

Using getter/setter to set the values in the labels of the widgets’ next instancies doesn’t obviously work as I need, because when I change any x.value, the following instancies’ start and end will be actually updated only when recalled, AFAIU.
I could connect every new widget to its previous (with valueChanged()) or create a function which finds the subsequent widget and update their properties, but that’s not a good solution.

Since I am almost new to python (most important: I’m not a programmer) I think that I am ignoring something about “connecting” variables in a better and cleanest way (maybe related to signals or threading?).

Consider that those widgets will actually be children widgets of another “main” widget, which will have similar properties: start taken from its previous main widget (if any), bars which is the sum of all bars in every children widget, end which will be again start+bars-a.

Thanks!

(I hope you will understand what I meant, my english is not perfect and, since I’m not a programmer, my terminology is not always correct)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:58:19+00:00Added an answer on June 18, 2026 at 4:58 am

    I can’t find use case for things from your question, but here is possible solution using Qt Signals-Slots:

    # -*- coding: utf-8 -*-
    import functools
    
    from PyQt4 import QtGui, QtCore
    
    
    class ObservableVariable(QtCore.QObject):
        """ Represents variable with value, when value changes it emits
        signal: changed(new_value)
        """
        changed = QtCore.pyqtSignal(object)
    
        def __init__(self, initial_value=0):
            super(ObservableVariable, self).__init__()
            self._value = initial_value
    
        def get_value(self):
            return self._value
    
        def set_value(self, new_val):
            self._value = new_val
            self.changed.emit(new_val)
    
        value = property(get_value, set_value)
    
        def __str__(self):
            return str(self.value)
    
        # it can support more operators if needed
        def __iadd__(self, other):
            self.value += other
            return self
    
        def __isub__(self, other):
            self.value -= other
            return self
    
    
    class MyClass(object):
        def __init__(self, referrer=None, value=1):
            self.referrer = referrer
            self.value = ObservableVariable(value)
            self._initial_value = value
            if referrer:
                # propagate referrer changes to subscribers
                referrer.value.changed.connect(
                    lambda x: self.value.changed.emit(self.value.value)
                )
    
        @property
        def start(self):
            if not self.referrer:
                return self.value.value
            return self.referrer.end + 1
    
        @property
        def end(self):
            return self.start + self.value.value - 1
    
    
    class GuiExample(QtGui.QWidget):
        def __init__(self):
            super(GuiExample, self).__init__()
            self.values = []
            layout = QtGui.QVBoxLayout(self)
            obj = None
            for i in range(5):
                obj = MyClass(obj, i)
                self.values.append(obj)
                # create gui elements
                hlayout = QtGui.QHBoxLayout()
                spinbox = QtGui.QSpinBox()
                spinbox.setValue(obj.value.value)
                start_label = QtGui.QLabel()
                end_label = QtGui.QLabel()
                hlayout.addWidget(start_label)
                hlayout.addWidget(spinbox)
                hlayout.addWidget(end_label)
                layout.addLayout(hlayout)
    
                # function called on value change
                def update_start_end(instance, start_label, end_label):
                    start_label.setText(str(instance.start))
                    end_label.setText(str(instance.end))
    
                action = functools.partial(update_start_end, obj, start_label,
                                           end_label)
                action()  # set initial start end text to labels
                # connect signals to gui elements
                obj.value.changed.connect(action)
                spinbox.valueChanged.connect(obj.value.set_value)
                obj.value.changed.connect(spinbox.setValue)
            layout.addWidget(QtGui.QPushButton('test',
                                               clicked=self.test_modification))
    
        def test_modification(self):
            self.values[1].value += 1
    
    
    app = QtGui.QApplication([])
    gui = GuiExample()
    gui.show()
    
    app.exec_()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.