所以我是python和PyQt的新手。我写了两个单独的文件:
我注意到每次单击按钮1,我已经链接到在文件2中运行test()函数时,我的GUI就会挂起。最后,我想编程按钮1以在单击时运行函数test(),然后如果用户按下按钮2,它将发送一个我已编码到其中的数字。
但是,我想知道raw_input是否可以这样使用。
档案1.
import sys
from PyQt4.QtCore import pyqtSlot
from PyQt4.QtGui import *
import user_input
# ----- GENERAL VARIABLES -------
# Windows Size
w_xSize = 200
w_ySize = 300
# Create a generic btn size
x_size = 130
y_size = 32
# Create generic x,y, variables to append to btn locations
# such that you can shift the entire btn locations as a whole
x_frame1 = (w_xSize-x_size)/2 #center
y_frame1 = 10
# ------ DEFINING WINDOW PROPERTIES -------
# create our window
app = QApplication(sys.argv)
# Base class to all our widget is QWidget
w = QWidget()
# Set window title
w.setWindowTitle('Primitive GUI')
# Set window size.
w.resize(w_xSize,w_ySize )
# ------ DEFINING BUTTON PROPERTIES -------
# Button 1
btn = QPushButton("Channel 0", w)
btn.move(0+x_frame1, 0+y_frame1)
btn.resize(x_size,y_size) #(x,y)
# Button 2
btn2 = QPushButton("Channel 1", w)
btn2.move(0+x_frame1, 30+y_frame1)
btn2.resize(x_size,y_size)
# Button 3
btn3 = QPushButton("Channel 2", w)
btn3.move(0+x_frame1, 60+y_frame1)
btn3.resize(x_size,y_size)
# Button 4
btn4 = QPushButton("Channel 3", w)
btn4.move(0+x_frame1, 90+y_frame1)
btn4.resize(x_size,y_size)
# ------ DEFINING TEXTBOX PROPERTIES -------
textbox = QLineEdit(w)
textbox.move(x_frame1, w_ySize-50)
textbox.resize(x_size,y_size)
# ------ CREATE SLOTS FOR BUTTONS ------
@pyqtSlot()
def on_click():
print("btn1 was clicked")
user_input.test()
@pyqtSlot()
def on_click_btn2():
print('btn2 was clicked')
textbox.setText("Button 2")
# ------ CONNECT SIGNALS TO THE SLOTS ------
# Each button can have its own function that it calls
# Button 1
btn.clicked.connect(on_click)
# Button 2
btn2.clicked.connect(on_click_btn2)
# ------ SHOW THE WINDOW ------
# Show the window and run the app
w.show()
app.exec_()
文件2
def test():
value = True
while (value == True):
user_input = raw_input("enter your command or type exit:")
if user_input.isdigit():
print "is a digit"
elif (user_input == "exit"):
value = False
else:
print "is not a digit"