Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # author: Bartlomiej "furas" Burek (https://blog.furas.pl)
- # 2016.10.26
- # https://www.reddit.com/r/learnpython/comments/59gzue/pyqt5_time_library_while_loop_freezes_window/
- # http://pastebin.com/eDeMdrAA
- import sys, time
- from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QRadioButton, QPushButton
- from PyQt5.QtGui import QIcon, QFont
- from PyQt5.QtCore import QTimer
- class Widget(QWidget):
- def __init__(self, parent=None):
- QWidget.__init__(self, parent)
- self.widget_layout = QVBoxLayout()
- # Making buttons
- self.radio1 = QPushButton('Radio 1')
- self.radio2 = QPushButton('Radio 2')
- # Making the text window
- self.line_edit = QLineEdit()
- font = self.line_edit.font()
- font.setPointSize(32)
- self.line_edit.setFont(font)
- # When the buttons get clicked
- self.radio1.clicked.connect(self.radio1_clicked) #[bool] makes it a toggle button?
- self.radio2.clicked.connect(self.radio2_clicked)
- # Place the buttons and line edit onto the screen.
- self.widget_layout.addWidget(self.radio1)
- self.widget_layout.addWidget(self.radio2)
- self.widget_layout.addWidget(self.line_edit)
- self.setWindowTitle('While loop test') # Window name
- self.setWindowIcon(QIcon('violin.png')) # Icon used by the window.
- self.setLayout(self.widget_layout)
- self.test = 0
- self.timeout = time.time() + 60*5 # 5 minutes from now
- self.timer = QTimer()
- self.timer.timeout.connect(self.update_time)
- self.timer.start(1000)
- def radio1_clicked(self):
- self.line_edit.setText('Radio 1')
- def radio2_clicked(self):
- self.line_edit.setText('Radio 2')
- def update_time(self):
- if self.test == 5 or time.time() > self.timeout:
- self.timer.stop()
- return
- self.test += 1
- widget.line_edit.setText(str(self.test))
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- widget = Widget()
- widget.show()
- #widget.line_edit.setText('Text updated!')
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement