python怎么用一个变量tm调用一个类的自定义函数的dj.(self)中的self.lineEdit.text()文本,然后导入while True:if 1==(tm):break
时间: 2023-02-27 18:45:33 浏览: 79
在 Python 中,如果你想通过一个变量 `tm` 调用一个类的自定义函数中的 `self.lineEdit.text()` 文本,你需要先实例化这个类,然后通过变量引用实例的方法,代码类似如下:
```
class MyClass:
def custom_function(self):
tm = self.lineEdit.text()
return tm
my_instance = MyClass()
tm = my_instance.custom_function()
while True:
if 1 == tm:
break
```
请注意,在这个代码示例中,我假设 `self.lineEdit.text()` 返回了一个数字 1,如果它返回了其他类型的值,则可能需要进行类型转换才能比较。
相关问题
import sys import threading import time from PyQt5.QtWidgets import * from PyQt5 import uic import pandas as pd import random # import pyqtgraph as pg import matplotlib.pyplot as plt from PyQt5.QtWidgets import QGroupBox from PyQt5 import QtWidgets from login_4 import Ui_CK from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas # df = pd.read_excel('shu.xlsx') class MyWindow(QWidget and QMainWindow,Ui_CK): def __init__(self): super().__init__() self.init_ui() groupbox = QGroupBox('Title',self) # self.plot = pg.PlotWidget(enableAutoRange=True) # self.ui.verticalLayout.addWidget(self.plot) # self.curve = self.plot.plot() #self.ui = uic.loadUi("./login_4.ui") def init_ui(self): print('1.1') try: self.ui = uic.loadUi("./login_4.ui") #print(threading.current_thread()) #print(self.ui.__dict__) # print(self.ui.label) # print(self.ui.label.text()) # 查看ui文件中有哪些控件 # 提取要操作的控件 self.user_name_qwidget = self.ui.lineEdit # 单位输入框 self.password_qwidget = self.ui.lineEdit_2 # 二级单位输入框 self.zhicheng_qwidget = self.ui.lineEdit_3 # 职称输入框 self.jiaoyuan_qwidget = self.ui.lineEdit_4 # 教员输入框 self.login_btn = self.ui.pushButton # 登录抽课按钮 self.textBrowser = self.ui.textBrowser # 授课对象显示区域 # 绑定信号与槽函数 self.textBrowser_2 = self.ui.textBrowser_2 # 文本显示区域课程名称 self.textBrowser_3 = self.ui.textBrowser_3 # 文本显示区域课次 self.textBrowser_4 = self.ui.textBrowser_4 # 文本显示区域教研室 self.login_btn.clicked.connect(self.login) self.login_btna = self.ui.pushButton_2 self.login_btna.clicked.connect(lambda: self.plot_q()) except Exception as e: print(e.__class__.__name__, e) def login(self): print('1.2') """登录按钮的槽函数""" #print(self.user_name_qwidget.text()) a = self.user_name_qwidget.text() e = sel 为什么会报错
这是一段Python代码,通过使用import关键字导入了sys、threading、time等模块。同时,也导入了PyQt5.QtWidgets的部分类和PyQt5.uic模块。此外,还导入了pandas和random模块,用于处理数据和生成随机数等操作。
import sys from PyQt5.QtWidgets import QApplication, QWidget , QMainWindow from Calui1 import Ui_Form class Mywindow(QWidget,Ui_Form): def init(self): super(Mywindow,self).init() self.setupUi(self) def btnClear_clicked(self): self.lineEdit.clear() def btn1(self): self.lineEdit.insert("1") def btn2(self): self.lineEdit.insert("2") def btn3(self): self.lineEdit.insert("3") def btn4(self): self.lineEdit.insert("4") def btn5(self): self.lineEdit.insert("5") def btn6(self): self.lineEdit.insert("6") def btn7(self): self.lineEdit.insert("7") def btn8(self): self.lineEdit.insert("8") def btn9(self): self.lineEdit.insert("9") def btn0(self): self.lineEdit.insert("0") def btn_plus(self): self.lineEdit.insert("+") def btn_sub(self): self.lineEdit.insert("-") def btn_mul(self): self.lineEdit.insert("*") def btn_div(self): self.lineEdit.insert("/") def btnequal(self): text=self.lineEdit.text() self.lineEdit.insert("= %.2f" %(eval(text))) if name=='main': app=QApplication(sys.argv) win=Mywindow() win.pushButton_7.clicked.connect(win.btnClear_clicked()) win.pushButton.clicked.connect(win.btn1()) win.pushButton_2.clicked.connect(win.btn2()) win.pushButton_3.clicked.connect(win.btn3()) win.pushButton_4.clicked.connect(win.btn_plus()) win.pushButton_5.clicked.connect(win.btn4()) win.pushButton_6.clicked.connect(win.btn7()) win.pushButton_8.clicked.connect(win.btn5()) win.pushButton_9.clicked.connect(win.btn8()) win.pushButton_10.clicked.connect(win.btn0()) win.pushButton_11.clicked.connect(win.btn6()) win.pushButton_12.clicked.connect(win.btn9()) win.pushButton_13.clicked.connect(win.btnequal()) win.pushButton_14.clicked.connect(win.btn_sub()) win.pushButton_15.clicked.connect(win.btn_mul()) win.pushButton_16.clicked.connect(win.btn_div()) win.show() sys.exit(app.exec_())为什么会报错
这段代码有几个问题:
1. 在第一行导入语句中,`from PyQt5.QtWidgets import QApplication, QWidget , QMainWindow` 中间缺少逗号,应该为 `from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow`。
2. 在 `Mywindow` 类的构造函数中,`init` 方法应该为 `__init__`。
3. 在 `Mywindow` 类中,所有的按钮槽函数应该以 `on_` 开头,例如 `on_btnClear_clicked`。
4. 在 `btnequal` 方法中,如果用户输入了非法表达式,可能会引发 `ZeroDivisionError` 异常,建议添加异常处理代码。
修改后的代码如下:
```
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from Calui1 import Ui_Form
class Mywindow(QWidget,Ui_Form):
def __init__(self):
super(Mywindow,self).__init__()
self.setupUi(self)
def on_btnClear_clicked(self):
self.lineEdit.clear()
def on_btn1_clicked(self):
self.lineEdit.insert("1")
def on_btn2_clicked(self):
self.lineEdit.insert("2")
def on_btn3_clicked(self):
self.lineEdit.insert("3")
def on_btn4_clicked(self):
self.lineEdit.insert("4")
def on_btn5_clicked(self):
self.lineEdit.insert("5")
def on_btn6_clicked(self):
self.lineEdit.insert("6")
def on_btn7_clicked(self):
self.lineEdit.insert("7")
def on_btn8_clicked(self):
self.lineEdit.insert("8")
def on_btn9_clicked(self):
self.lineEdit.insert("9")
def on_btn0_clicked(self):
self.lineEdit.insert("0")
def on_btn_plus_clicked(self):
self.lineEdit.insert("+")
def on_btn_sub_clicked(self):
self.lineEdit.insert("-")
def on_btn_mul_clicked(self):
self.lineEdit.insert("*")
def on_btn_div_clicked(self):
self.lineEdit.insert("/")
def on_btnequal_clicked(self):
try:
text = self.lineEdit.text()
result = eval(text)
self.lineEdit.setText("= %.2f" % result)
except ZeroDivisionError:
self.lineEdit.setText("除数不能为零")
except Exception as e:
self.lineEdit.setText(str(e))
if __name__=='__main__':
app = QApplication(sys.argv)
win = Mywindow()
win.pushButton_7.clicked.connect(win.on_btnClear_clicked)
win.pushButton.clicked.connect(win.on_btn1_clicked)
win.pushButton_2.clicked.connect(win.on_btn2_clicked)
win.pushButton_3.clicked.connect(win.on_btn3_clicked)
win.pushButton_4.clicked.connect(win.on_btn_plus_clicked)
win.pushButton_5.clicked.connect(win.on_btn4_clicked)
win.pushButton_6.clicked.connect(win.on_btn7_clicked)
win.pushButton_8.clicked.connect(win.on_btn5_clicked)
win.pushButton_9.clicked.connect(win.on_btn8_clicked)
win.pushButton_10.clicked.connect(win.on_btn0_clicked)
win.pushButton_11.clicked.connect(win.on_btn6_clicked)
win.pushButton_12.clicked.connect(win.on_btn9_clicked)
win.pushButton_13.clicked.connect(win.on_btnequal_clicked)
win.pushButton_14.clicked.connect(win.on_btn_sub_clicked)
win.pushButton_15.clicked.connect(win.on_btn_mul_clicked)
win.pushButton_16.clicked.connect(win.on_btn_div_clicked)
win.show()
sys.exit(app.exec_())
```
希望能帮到你!
阅读全文