class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.connect_mysql.clicked.connect(self.on_click)
self.dialog = MainMenu()
def on_click(self):
try:
ip = self.ip.text()
user = self.user.text()
password = self.password.text()
port = self.port.text()
db = self.db.text()
mydb = mysql.connector.connect(
host=ip,
user=user,
passwd=password,
database=db
)
mycursor = mydb.cursor()
self.close()
self.dialog.show()
except:
QMessageBox.about(self, "Message", "Something went wrong...")
我想从另一个类访问mucursor。有可能吗?代码关闭窗口,并打开发送mysql查询的新窗口。
答案 0 :(得分:0)
是的,有可能,所有对象属性在Python中都是公共的。您所需要做的就是将光标保存在类实例中:
self.mycursor = mycursor = ...
然后从其他地方
my_window_instance.mycursor
答案 1 :(得分:0)
进行定义,以获取ExampleApp类中的变量:
def get_mycursor(self):
return self.mycursor
如果要访问mycursor重新定义它,则还可以设置一个函数:
def set_mycursor(self, newval):
self.mycursor = newval