0
我試圖通過在程序結束時使用QtGui.QLineEdit.setText()來設置一些字符串,並在程序開始時使用QtGui.QLineEdit.clear()清除字符串。但是,它從不清除字符串。我想要做的就是清除程序開始處的內容,指示它正在運行,並在程序結束時設置程序的值和狀態。有沒有任何建議或其他方式來實現此功能?非常感謝你。Python Pyqt QtGui.QLineEdit.clear()
下面是我的代碼:(我省略了一些其他不相關的定義和功能)
def __init__(self):
self.left = QtGui.QFrame()
self.read_back_B0_to_B39 = QtGui.QTextEdit(self.left)
self.first_read_Value_error = QtGui.QLineEdit(self.left)
self.B0_B39_match = QtGui.QLineEdit(self.left)
def first_burn_margin_read(self):
# try to clear the content
self.first_read_Value_error.clear()
self.read_back_B0_to_B39.clear()
self.B0_B39_match.clear()
# E3646 power supply setup
power_channel = 1; power_range = 'LOW'; power_voltage = 3.3; power_current = 0.3
self.power1_setup(power_channel, power_range, power_voltage, power_current)
self.power1_on_off(False)
self.power1_on_off(True)
# E3631 power supply setup
power_channel = 2; power_voltage = 7; power_current = 0.3
self.power2_setup(power_channel, power_voltage, power_current)
self.power2_on_off(False)
self.power2_on_off(True)
# 81130A setup
fun_channel = 1; fun_frequency = 100; fun_duty_cycle = 50; fun_vlow = 0; fun_vhigh = 0.8
self.fun_setup(fun_channel, fun_frequency, fun_duty_cycle, fun_vlow, fun_vhigh)
self.fun_on_off(fun_channel, 1)
time.sleep(1)
value = self.Byte0_to_Byte39.toPlainText()
write_value = []*40
for i in range(0,40):
hex_express = str(value[5*i:5*i+4])
hex_int = int(hex_express, 16)
write_value.append(hex_int)
self.i2c_write(128, write_value)
time.sleep(2)
try:
burn_result = self.i2c_read(128, 40)
self.first_read_Value_error.setText('No')
if write_value[1:] == burn_result:
B46 = int(str(self.B46_OTP_start_address_1.text()), 16)
B47 = int(str(self.B47_OTP_stop_address_1.text()), 16)
B48 = int(str(self.B48_Ram_burn_start_address_1.text()), 16)
burn_registers = [B46, B47, B48]
self.i2c_write(174, burn_registers)
time.sleep(0.1)
# # Burn OTP
self.i2c_write(173, [0x8C])
time.sleep(0.5)
self.i2c_write(173, [0x0C])
time.sleep(0.5)
# Margin 1 read
self.i2c_write(128, [0x00]*40)
time.sleep(0.5)
self.i2c_write(177, [0x00])
time.sleep(0.1)
self.i2c_write(173, [0x4C])
time.sleep(0.1)
self.i2c_write(173, [0x0C])
time.sleep(1)
margin_read = self.i2c_read(128, 40)
row1 = ''
row2 = ''
row3 = ''
row4 = ''
for i in range(0,10):
row1 += str(hex(margin_read[i])) + ' '
row2 += str(hex(margin_read[i+10])) + ' '
row3 += str(hex(margin_read[i+20])) + ' '
row4 += str(hex(margin_read[i+30])) + ' '
read_back = """%s
%s
%s
%s
""" % (row1, row2, row3, row4)
self.read_back_B0_to_B39.setText(read_back)
if write_value[1:] != margin_read:
self.log.warn('The burning is not correct. Please check the setup and do it again.')
self.B0_B39_match.setText('It is not matched.')
else:
self.log.info('1st pass burn and margin 1 read are successful, please go on.')
self.B0_B39_match.setText('It is matched.')
else:
self.log.warn('The burning is not correct. Please check the setup and do it again.')
except ValueError:
self.first_read_Value_error.setText('Yes')
如果在GUI線程中調用'first_burn_margin_read'(例如響應按鈕單擊),則只有在方法返回後GUI纔會更新,因此在運行時不會看到任何更改,而且GUI將被阻止。您應該閱讀[this](http://doc.qt.io/qt-5/thread-basics.html)以瞭解Qt中的線程是如何工作的。 – mata
對不起,你和我的其他程序在這個方法中的作品之一。所以我很困惑,你有什麼建議來實現我想做的事情嗎?非常感謝你。 – Dogod
像這樣的長時間運行的函數應該在一個單獨的線程中運行,並且應該使用Qts信號/槽機制來更新GUI,因爲Qt窗口小部件類中的方法不應該從單獨的線程調用。 [此鏈接](http://doc.qt.io/qt-5/threads-technologies.html)對Qt中的可用線程技術有更詳細的概述。 – mata