开发者

PyQt, trying to make LCD qt widget countdown

开发者 https://www.devze.com 2023-03-28 05:02 出处:网络
I have a project I\'m working on where I have to program an elevator \"simulator\". I am using python and pyqt for the first time and having some difficulty.

I have a project I'm working on where I have to program an elevator "simulator". I am using python and pyqt for the first time and having some difficulty. At the moment I have 2 classes, one is the elevator class开发者_如何学Python, other is the liftUI class which has a QLCDNumber widget. I have the elevator class set up to send the current floor and the destination floor numbers to the liftUI class so I thought I could just use a while loop to have the lcd value change until current floor and destination floor are the same, but it just freezes and I have to force close.

Here is the code I have at the moment:

    def lcdFloorNum(self, floorNum, curFloor):
    if floorNum <= curFloor:
        while floorNum <= curFloor:
            self.ui.floorNumber.setProperty("value", floorNum)
            floorNum -= 1

This is just for when the elevator is going down obviously, I'll do "going up" once this works... It gets the floorNum and curFloor from another function in the elevator class which I'm pretty sure is working fine. If I get rid of the while loop, it will update the number, just once and not until the end. Is there some better code to achieve what I want that wont freeze the GUI?


I think you want to decrement curFloor don't you? Your while statement is while floorNum <= curFloor:. If you decrement floorNum, that statement will always be true.


In addition to the other problems mentioned here, your code is changing the number displayed in the UI without any time in between steps (and indeed without allowing the GUI to update), so it will "count down" so fast you can't see it.

A better approach might be to use a QTimer so you can simulate, e.g. going down one floor every second or similar. Take a look at QTimer and the signal/slot mechanism. I found a small PyQT example of this here : http://www.rkblog.rk.edu.pl/w/p/qtimer-making-timers-pyqt4/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号