由于循环,pyqt4 QTextEdit在我的代码中不起作用

时间:2016-02-29 00:24:30

标签: python python-3.x while-loop pyqt4 qtextedit

我有这个代码可以创建一个文本编辑窗口:

class Main(QtGui.QMainWindow):
    def __init__(self, parent = None):
            QtGui.QMainWindow.__init__(self, parent)

            self.initUI()
            print("just started the ui")

    def initUI(self):
            print("i am in initUi")
            self.text = QtGui.QTextEdit(self)
            self.setCentralWidget(self.text)
            # x and y coordinates on the screen, width, height
            self.setGeometry(100,100,1030,800)

            self.setWindowTitle("Writer")
            sleep(1)

write_msg()函数正常,并按预期将文本写入终端。但现在我想用这个函数在QTextEdit里面写。问题是write_msg()内部有一个while循环如果我有while循环窗口永远不会弹出,如果我删除while循环窗口弹出,编辑器就在那里我可以从我的键盘输入任何东西,但我的功能不能工作虽然它是基础。

这是我的write_msg()功能:

def write_msg():
    print("i am in write_msg function")
    #Each analog sensor has some characters to roll
    sensor16=['1','-','\\','/','*','!']
    sensor15=['4','G','H','I']
    sensor14=['7','P','Q','R','S']
    sensor13=['*']
    sensor12=['2','A','B','C']
    sensor11=['5','J','K','L']
    sensor10=['8','T','U','V']
    sensor09=['0',' ']
    sensor08=['3','D','E','F']
    sensor07=['6','M','N','O']
    sensor06=['9','W','X','Y','Z']
    sensor05=['#']
    sensor04=['BACKSPACE']
    sensor03=['DELETE ALL']
    sensor02=['READ']
    sensor01=['TRANSMITE']
    sensor=[sensor01,sensor02,sensor03,sensor04,sensor05,sensor06,sensor07,sensor08,sensor09,sensor10,sensor11,sensor12,sensor13,sensor14,sensor15,sensor16]
    #the maximum number of times each sensor can be pressed
    #before it rols back to the first character.
    max_press=[1,1,1,1,1,5,4,4,2,4,4,4,1,5,4,6]
    num_press=0
    message=[]
    steps=0
    i=0
    x=0
    key=0
    key_pressed=0
    #message_string="kjsdfgaqlkfvbnajkefnvbsfejfhvbjhkefrbvksjehdjefbv"
    #print(message_string)
    #p1 = subprocess.Popen(["minimodem" , '--tx' , '300'], stdin=subprocess.PIPE)
    #p1.stdin.write(bytes(message_string, 'UTF-8'))        
    while state == "wrt":
            print("i am looping")
            binary_x="{0:04b}".format(x)
            GPIO.output(15, int(binary_x[0]))
            GPIO.output(13, int(binary_x[1]))
            GPIO.output(11, int(binary_x[2]))
            GPIO.output(7, int(binary_x[3]))
            # average three readings to get a more stable one
            channeldata_1 = read_mcp3002(0) # get CH0 input
            sleep(0.001)
            channeldata_2 = read_mcp3002(0) # get CH0 input
            sleep(0.001)
            channeldata_3 = read_mcp3002(0) # get CH0 input
            channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
            #
            # Voltage = (CHX data * (V-ref [= 3300 mV] * 2 [= 1:2 input divider]) / 1024 [= 10bit resolution]            #
            voltage = int(round(((channeldata * vref * 2) / resolution),0))+ calibration
            #print(voltage)
            if DEBUG : print("Data (bin)    {0:010b}".format(channeldata))
            #key_pressed=x
            if x==15 :      # some problem with this sensor so i had to go and twicked the thresshold
                    voltage = voltage - 500
            #time.sleep(0.05)
            if ( voltage > 2500) :   #key is released
                    keypressed = False
                    keyreleased = True
                    x=x+1
            if ( voltage <= 2500) :   #key is pressed
                    keypressed = True
                    keyreleased = False
                    key_pressed=x#define which key is pressed

            if key_pressed==0 and key!=0:
                    transmite(message)
                    sleep(0.01)
                    x=x+1
            if key_pressed==1:
                    state == "rd_msg"
                    x=x+1
            if key_pressed==2:
                    sys.stdout.write('\033[2K')
                    sys.stdout.write('\033[1G')
                    message_len = len(message)
                    for m in range(message_len):
                            del message[m]
                    x=x+1
            if key_pressed==3:
                    #print('\b\b')
                    print('\b ', end="", flush=True) 
                    sys.stdout.write('\010')
                    message_len = len(message)
                    del message[message_len]
                    sleep(1)
                    x=x+1
            if key_pressed > 3:
                    print("i am pressing a number")
                    if key == key_pressed :
                            while num_press <= (max_press[key_pressed]) and keyreleased==False:
                                # average three readings to get a more stable one
                                channeldata_1 = read_mcp3002(0) # get CH0 input
                                sleep(0.001)
                                channeldata_2 = read_mcp3002(0) # get CH0 input
                                sleep(0.001)
                                channeldata_3 = read_mcp3002(0) # get CH0 input
                                channeldata = (channeldata_1+channeldata_2+channeldata_3)/3
                                #
                                # Voltage = (CHX data * (V-ref [= 3300 mV] * 2 [= 1:2 input divider]) / 1024 [= 10bit resolution]
                                #
                                voltage = int(round(((channeldata * vref * 2) / resolution),0))+ calibration
                                if DEBUG : print("Data (bin)    {0:010b}".format(channeldata))
                                if x==15 :         # some problem with this sensor so i had to go and twicked the thresshold
                                        voltage = voltage - 500
                                time.sleep(0.05)
                                if ( voltage > 2500) :     #key is released
                                    keyreleased = True
                                    keypressed = False
                                    sys.stdout.write('\033[1C')
                                    char=sensor[key_pressed][num_press-1]
                                    message.append(char)
                                    self.text.setText(char)
                                    num_press=0
                                else :
                                    keypressed = True
                                    keyreleased= False
                                if num_press <= max_press[key_pressed] and keyreleased == False:
                                    print(sensor[key_pressed][num_press], end="", flush=True) 
                                    sys.stdout.write('\010')
                                    num_press=num_press+1
                                    time.sleep(0.5)
                                    if num_press == max_press[key_pressed] :
                                            num_press=0
            if x == 16 :
                    x=0 
            key = key_pressed

我必须不断更改x的值,将其从0增加到16,以使此功能正常工作。有人有任何想法吗?

好的!我已经尝试使用线程,所以我可以运行我的write_msg()函数,但无法让它工作,如果我评论线程文本编辑器窗口正常弹出,没有发生任何事情,如果我取消注释它弹出输出此错误消息:

  

pi @ raspberrypi:〜$ sudo python3 ./Documents/Examples/texting_app.py   [xcb]处理回复时未知的序列号   [xcb]这很可能是一个多线程客户端,而且还没有调用XInitThreads   [xcb]中止,对不起。   python3:../../ src / xcb_io.c:635:_XReply:断言`!xcb_xlib_threads_sequence_lost'失败。

这是main()中用于启动所有内容的代码:

def main():
    #t = threading.Thread(target=write_msg)
    #t.daemon = True
    #t.start()
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

0 个答案:

没有答案