I have a python app with pyqt5 that change mac address in linux machine. I have two files. The first file is the main python program and second file is gui window with empty line that you have to fill in mac address then press ok.
Actually when I fill the mac address it say NameError: name 'connection' is not defined
I cannot pass data from second file to main python file. In mac_2.py
file there is a global var with name connection that cannot read from second file mac_specific.py. How can I do that?
I reduce much code from two files, because it is easiest for you to help me.
mac_2.py
#!/usr/bin/python3
import os
import subprocess
from PyQt5 import QtCore, QtGui, QtWidgets
from mac_specific import *
class Ui_MainWindow(object):
def specificWindow(self):
self.window = QtWidgets.QDialog()
self.ui = Ui_macSpecific()
self.ui.setupUi(self.window, self) # <--- assign MainWindow as parent to second window
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setFixedSize(430, 500)
# ...
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.specificWindow())
# ...
def select(self):
global connection
if self.radioButton.isChecked():
self.wired = config.get('ethernet', 'wired')
self.label.setText(str(self.wired))
connection = self.wired
self.mac_address = config.get('ethernet', 'wired_mac_address')
self.label_2.setText(str(self.mac_address))
if self.radioButton_2.isChecked():
self.wireless = config.get('wifi', 'wireless')
self.label.setText(str(self.wireless))
connection = self.wireless
self.mac_address = config.get('wifi', 'wireless_mac_address')
self.label_2.setText(str(self.mac_address))
mac_specific.py
import subprocess
from PyQt5 import QtCore, QtGui, QtWidgets
# read values from a section
wired = config.get('ethernet', 'wired')
wireless = config.get('wifi', 'wireless')
class Ui_macSpecific(object):
def setupUi(self, macSpecific, master):
self.master = master
self.window = macSpecific
macSpecific.setObjectName("macSpecific")
macSpecific.resize(301, 140)
self.gridLayout = QtWidgets.QGridLayout(macSpecific)
# ...
self.pushButton = QtWidgets.QPushButton(macSpecific, clicked = lambda: self.clicked_button())
def clicked_button(self):
print("submit")
text = self.lineEdit.text()
self.master.label_2.setText(text)
subprocess.run(['pkexec', 'ifconfig', connection, 'down'])
subprocess.run(['pkexec', 'ip', 'link', 'set', 'dev', connection, 'address', text])
subprocess.run(['pkexec', 'ifconfig', wired, 'up'])
self.window.close()