3

This is my first question on StackOverflow. I have always found what I was looking for just googling, but this time I'm stuck and can't figure it out.

I'm a beginner programmer with python and still learning a lot.

I want to change a dateEdit box in a Userinterface with a small code to set det current date time. the code looks like this.

self.dateEdit_2.setDateTime(QtCore.QDateTime.currentDateTime())

Now i want to change every dateEdit box the same, starting from 2 and going to 29, without typing every single line out.

i have tried to make a for loop with a filled list. and i get it to print out what i want, but how does i get "set_date_numb" to be a attribute that does what i want.

hope you understand, Thanks.

    dateTimeList = ['2','3','4','5','6','7','8','9',
    '10','11','12','13','14','15','16','17','18','19','20',
    '21','22','23','24','25','26','27','28','29']
    indexval = 0
    for i in range(len(dateTimeList)):
        date_numb = (dateTimeList[indexval])
        set_date_numb ='self.dateEdit_{}.setDateTime(QtCore.QDateTime.currentDateTime())'.format(date_numb)
        print(set_date_numb)
        indexval += 1
1
  • Hey, I saw your earlier answer that said my answer helped you. I'm glad it worked out. If it did work, would you mind accepting my answer? Commented Sep 24, 2018 at 14:01

1 Answer 1

2

You could use getattr(), see the documentation here. Since the functions you are after are members of your instance you can grab them with their names as strings (which I think is the main problem you are facing):

dateTimeList = [str(x) for x in range(2,30)]

for dt in dateTimeList:
    name = "dateEdit_{}".format(dt)
    currentDateEdit = getattr(self, name)
    currentDateEdit.setDateTime(QtCore.QDateTime.currentDateTime())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.