使用 PyQt5 设计器将按钮集成到现有代码中
Using PyQt5 designer to integrate a pushbutton to existing code
from pages import*
import time
import sys
#GPIO Pins Setup
buzzer_motor = 12
#input from physical switch
button = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_UP)
#output to motor which is connected to pin 12
GPIO.setup(buzzer_motor, GPIO.OUT)
a = 0
def Mag_Train():
GPIO.output(buzzer_motor, True)
time.sleep(.3)
GPIO.output(buzzer_motor, False)
result = "Success"
print(time.asctime())
time_end = time.asctime()
time.sleep(1)
return [time_end,result]
while(a == 0):
if(GPIO.input(button) == False):
Mag_Train()
# def sayHello():
# print("Push Button Clicked")
# button = True
#
# app = QApplication(sys.argv)
# magazineStart = magazineStart("MAGAZINE START")
# magazineStart.clicked.connect(sayHello)
# magazineStart.show()
# app.exec_()
所以我正在尝试创建一个运行上面程序的按钮。我以前使用的是物理按钮,但现在我想做一个数字显示。我已经在 QT5 设计器上创建了按钮,但似乎无法集成它
TL;DR 您将 现有代码 集成到 UI,而不是相反。如果您只需要一个按钮,直接在 python 中编写 ui 会更容易,但是由于您已经有了 ui
文件,所以这个答案将指向那个方向。
对 PyQt5 使用 pyside2-uic
或 pyuic
- 我想 - 转换 .ui
文件以将其转换为 python 代码。
例如,在 windows cmd 与 PySide2 的情况下:
pyside2-uic main.ui > ui_main.py
将 main.ui
更改为相应的 .ui
文件位置。
要使用生成的 python 脚本,最好导入并创建它的子类,以备日后可能进行的更改。
以下示例假设您只有一个名为 pushButton
的按钮,并用作打开或关闭功能的切换按钮。
仅仅 copy-paste 你的逻辑到 function_to_be_run
,将 pushButton
更改为你的 QPushButton 的名称并更改 time.sleep()
调用event.wait()
开箱即用,但我建议先学习代码。
from PySide2.QtWidgets import QMainWindow, QApplication
from ui_main import Ui_MainWindow
import threading
import sys
def function_to_be_run(event: threading.Event):
while not event.is_set():
# Do your stuff here. We'll just print and wait using event.wait().
print("Alive 'n kickin'")
event.wait(1)
print('Good bye!')
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.released.connect(self.starter)
def stopper(self, thread_event: threading.Event, thread: threading.Thread):
thread_event.set()
thread.join() # Wait for thread to finish
self.pushButton.released.connect(self.starter) # connect button back to starter
def starter(self):
event = threading.Event()
t = threading.Thread(target=function_to_be_run, args=[event])
t.start()
self.pushButton.released.connect(lambda x: self.stopper(event, t)) # connect to stopper so it can stop.
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
请注意:
- 两个 Qt 实现支持
Qrunnable
处理并发 运行 内部可调用的任一个,但看起来你的任务并不复杂并且使用 threading
直接模块会更直接
- Re-connecting 按钮操作可以通过在 Desiger 程序中启用 QPushButton 中的 Toggle 属性来避免,并使用新函数来检查按钮是否被切换 - 但你必须在本地之外存储对
thread
和 event
的引用。
def if_button_is_toggle(self):
if self.pushButton.isChecked():
pass # stop thread here
else:
pass # run thread here
如果您只有 2 个按钮 - 开始、停止,会更容易。
from pages import*
import time
import sys
#GPIO Pins Setup
buzzer_motor = 12
#input from physical switch
button = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_UP)
#output to motor which is connected to pin 12
GPIO.setup(buzzer_motor, GPIO.OUT)
a = 0
def Mag_Train():
GPIO.output(buzzer_motor, True)
time.sleep(.3)
GPIO.output(buzzer_motor, False)
result = "Success"
print(time.asctime())
time_end = time.asctime()
time.sleep(1)
return [time_end,result]
while(a == 0):
if(GPIO.input(button) == False):
Mag_Train()
# def sayHello():
# print("Push Button Clicked")
# button = True
#
# app = QApplication(sys.argv)
# magazineStart = magazineStart("MAGAZINE START")
# magazineStart.clicked.connect(sayHello)
# magazineStart.show()
# app.exec_()
所以我正在尝试创建一个运行上面程序的按钮。我以前使用的是物理按钮,但现在我想做一个数字显示。我已经在 QT5 设计器上创建了按钮,但似乎无法集成它
TL;DR 您将 现有代码 集成到 UI,而不是相反。如果您只需要一个按钮,直接在 python 中编写 ui 会更容易,但是由于您已经有了 ui
文件,所以这个答案将指向那个方向。
对 PyQt5 使用 pyside2-uic
或 pyuic
- 我想 - 转换 .ui
文件以将其转换为 python 代码。
例如,在 windows cmd 与 PySide2 的情况下:
pyside2-uic main.ui > ui_main.py
将 main.ui
更改为相应的 .ui
文件位置。
要使用生成的 python 脚本,最好导入并创建它的子类,以备日后可能进行的更改。
以下示例假设您只有一个名为 pushButton
的按钮,并用作打开或关闭功能的切换按钮。
仅仅 copy-paste 你的逻辑到 function_to_be_run
,将 pushButton
更改为你的 QPushButton 的名称并更改 time.sleep()
调用event.wait()
开箱即用,但我建议先学习代码。
from PySide2.QtWidgets import QMainWindow, QApplication
from ui_main import Ui_MainWindow
import threading
import sys
def function_to_be_run(event: threading.Event):
while not event.is_set():
# Do your stuff here. We'll just print and wait using event.wait().
print("Alive 'n kickin'")
event.wait(1)
print('Good bye!')
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.released.connect(self.starter)
def stopper(self, thread_event: threading.Event, thread: threading.Thread):
thread_event.set()
thread.join() # Wait for thread to finish
self.pushButton.released.connect(self.starter) # connect button back to starter
def starter(self):
event = threading.Event()
t = threading.Thread(target=function_to_be_run, args=[event])
t.start()
self.pushButton.released.connect(lambda x: self.stopper(event, t)) # connect to stopper so it can stop.
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
请注意:
- 两个 Qt 实现支持
Qrunnable
处理并发 运行 内部可调用的任一个,但看起来你的任务并不复杂并且使用threading
直接模块会更直接 - Re-connecting 按钮操作可以通过在 Desiger 程序中启用 QPushButton 中的 Toggle 属性来避免,并使用新函数来检查按钮是否被切换 - 但你必须在本地之外存储对
thread
和event
的引用。
def if_button_is_toggle(self):
if self.pushButton.isChecked():
pass # stop thread here
else:
pass # run thread here
如果您只有 2 个按钮 - 开始、停止,会更容易。