V
V
Vitaly2014-12-14 21:12:53
Python
Vitaly, 2014-12-14 21:12:53

How to create an object (class, instance, button in gui) with a previously unknown name in Python, PyQt?

The script pulls from another file some data that is not known in advance.
and makes a dictionary out of them like:
info = {date: {finished: {id : time}}}
I would like to make a separate button in PyQt for each date (their number is not known in advance)
when clicking on which other details were shown
Please tell me how can this be done? This is how you need to dynamically set the names of the buttons. And somehow they need to be addressed after
ps and another second question, how can such data be written not in a dictionary, but in a class or instance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaliy, 2014-12-15
@vitosua

doper himself how to do it:

class MainWindow (QtGui.QWidget)

...

        self.buttons_list ={}
        for date in info:
            self.buttons_list[date] = QtGui.QPushButton(self.splitter)
        
            self.buttons_list[date].setObjectName(date)
            self.buttons_list[date].setText(date)
            QtCore.QObject.connect(self.buttons_list[date], QtCore.SIGNAL("clicked()"), self.tableWidget.clear)

it turned out that links to button instances are written as values ​​in the buttons_list[date] dictionary
and you can access each button by key.
As a result, the gui was displayed with the same number of buttons as date in info:

Z
zelsky, 2014-12-14
@zelsky

It's easier to use. kivy

def on_touch_down(self, touch):
    if super(OurClassName, self).on_touch_down(touch):
        return True
    if not self.collide_point(touch.x, touch.y):
        return False
    print('you touched me!')
    return True

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question