我很抱歉,如果這是一個簡單/愚蠢的錯誤或已經被回答,我很新的編程和我正在教育自己。這是我在stackoverflow中的第一個問題,所以請讓我知道如果你需要進一步的信息或我的問題不清楚。爲pyqt4行返回的空白值
我的代碼無法檢索到我在我的lineEdit字段中輸入的IP地址。在函數「readip」中,我試圖獲取lineEdit的值,但我從來沒有得到我輸入的值。我試過ip = str(gui.lineEdit.text())和ip = str(gui.lineEdit.text)。 ip = str(gui.lineEdit.text)爲我提供了對象的內存插槽,但不是IP地址。
我從我的應用程序附加代碼。請告訴我,我做錯了什麼
這是我的主要用途:
import Gui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import PyQt4
import subprocess
import time
import os
import sys
from threading import Thread
class Gui(QDialog,Gui.Ui_mGui):
def __init__(self,parent=None):
global ip
super(Gui,self).__init__(parent)
self.setupUi(self)
self.connect(self.instructionButton, SIGNAL("clicked()"), self.showInstructions)
Gui.connect(self.serviceButton, SIGNAL("clicked()"), backendServiceClass)
self.checkBox.stateChanged.connect(self.autostartService)
if self.checkBox.isChecked():
print "The AutoStartService Button was checked"
#backendService()
else:
print "The AutoStartService Button was not checked"
def readip(self):
global ip
gui=Gui()
ip=str(gui.lineEdit.text())
print ip
time.sleep(1)
print"test"
print ip
time.sleep(20)
def showInstructions(self):
QMessageBox.information(self,"Instructions","Enter the IP address of a mobile device you carry with you always.The Application would check whether it can reach that device using your wireless network. If the device is unreachable, it would put the computer to sleep!")
def autostartService(self):
if self.checkBox.isChecked():
autoStartService=1
TrackAutoStartService = open("AutoStartService", "w+")
TrackAutoStartService.write(str(autoStartService))
TrackAutoStartService.close
print "Autostart Service Checked"
print autoStartService
else:
autoStartService = 0
TrackAutoStartService = open("AutoStartService", "w+")
TrackAutoStartService.write(str(autoStartService))
TrackAutoStartService.close
print "Autostart Service Unchecked"
print autoStartService
class backendServiceClass(Thread,Gui):
def __init__(self,parent=None):
Thread.__init__(self)
self.daemon = True
self.start()
def run(Gui):
global loop
global ip
Gui.readip()
print ip
print ip
TrackIP = open("TrackIP", "w+")
TrackIP.write(ip)
TrackIP.close
if loop==1:
loop=0
#Gui.serviceButton.setText("Start Service")
else:
loop=1
#Gui.serviceButton.setText("Stop Service")
print("ping -n 1 -w 1000 " + ip)
down = 0
while loop==1:
try:
response = subprocess.check_output(
['ping', '-n', '1', '-w', '2000', ip],
stderr=subprocess.STDOUT, # get all output
universal_newlines=True, # return string not bytes
shell=True,
creationflags=subprocess.SW_HIDE
)
except subprocess.CalledProcessError:
response = None
if (response != None):
down = 0
time.sleep(1)
print ("The host" + ip + " is Alive!")
else:
down = down + 1
time.sleep(1)
print (down)
if (down == 10):
print ("a total of " + str(down) + "pings has been lost")
down = 0
time.sleep(10)
os.system("shutdown -h")
global loop
loop=0
app=QApplication(sys.argv)
form=Gui()
form.show()
app.exec_()
下面是Gui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Gui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_mGui(object):
def setupUi(self, mGui):
mGui.setObjectName(_fromUtf8("mGui"))
mGui.resize(246, 308)
mGui.setMinimumSize(QtCore.QSize(246, 308))
mGui.setMaximumSize(QtCore.QSize(246, 309))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8(":/image/logo.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
mGui.setWindowIcon(icon)
self.checkBox = QtGui.QCheckBox(mGui)
self.checkBox.setGeometry(QtCore.QRect(20, 140, 201, 20))
f = open("AutoStartService", "r+")
AutoStartService = int(f.read())
if AutoStartService == 1:
self.checkBox.setChecked(True)
else:
self.checkBox.setChecked(False)
self.checkBox.setObjectName(_fromUtf8("checkBox"))
self.SleeperLabel = QtGui.QLabel(mGui)
self.SleeperLabel.setGeometry(QtCore.QRect(90, 10, 131, 51))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Harlow Solid Italic"))
font.setPointSize(28)
font.setBold(False)
font.setItalic(True)
font.setWeight(50)
self.SleeperLabel.setFont(font)
self.SleeperLabel.setObjectName(_fromUtf8("SleeperLabel"))
self.SleeperLabel_2 = QtGui.QLabel(mGui)
self.SleeperLabel_2.setGeometry(QtCore.QRect(10, 10, 71, 61))
font = QtGui.QFont()
font.setFamily(_fromUtf8("Harlow Solid Italic"))
font.setPointSize(28)
font.setBold(False)
font.setItalic(True)
font.setWeight(50)
self.SleeperLabel_2.setFont(font)
self.SleeperLabel_2.setText(_fromUtf8(""))
self.SleeperLabel_2.setPixmap(QtGui.QPixmap(_fromUtf8(":/image/logo64x64.png")))
self.SleeperLabel_2.setObjectName(_fromUtf8("SleeperLabel_2"))
self.serviceButton = QtGui.QPushButton(mGui)
self.serviceButton.setGeometry(QtCore.QRect(10, 260, 101, 31))
self.serviceButton.setObjectName(_fromUtf8("serviceButton"))
self.instructionButton = QtGui.QPushButton(mGui)
self.instructionButton.setGeometry(QtCore.QRect(130, 260, 101, 31))
self.instructionButton.setObjectName(_fromUtf8("instructionButton"))
self.label = QtGui.QLabel(mGui)
self.label.setGeometry(QtCore.QRect(20, 210, 151, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(mGui)
self.label_2.setGeometry(QtCore.QRect(20, 80, 171, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.checkBox_2 = QtGui.QCheckBox(mGui)
self.checkBox_2.setGeometry(QtCore.QRect(20, 160, 221, 20))
self.checkBox_2.setObjectName(_fromUtf8("checkBox_2"))
self.label_3 = QtGui.QLabel(mGui)
self.label_3.setGeometry(QtCore.QRect(20, 190, 151, 16))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.lineEdit = QtGui.QLineEdit(mGui)
self.lineEdit.setGeometry(QtCore.QRect(20, 98, 181, 31))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(mGui)
QtCore.QMetaObject.connectSlotsByName(mGui)
def retranslateUi(self, mGui):
mGui.setWindowTitle(_translate("mGui", "Sleeper V3.0", None))
self.checkBox.setText(_translate("mGui", "Autostart Service at Application start", None))
self.SleeperLabel.setText(_translate("mGui", "Sleeper", None))
self.serviceButton.setText(_translate("mGui", "Start Service", None))
self.instructionButton.setText(_translate("mGui", "Instructions", None))
self.label.setText(_translate("mGui", "Currently Tracking the device :", None))
self.label_2.setText(_translate("mGui", "IP Address of your Mobile Device", None))
self.checkBox_2.setText(_translate("mGui", "Autostart Application at System Startup", None))
self.label_3.setText(_translate("mGui", "Service Status :", None))
f1 = open("TrackIP", "r+")
IP = str(f1.read())
if IP != "":
self.lineEdit.setText(_translate("mGui", IP, None))
self.lineEdit.setPlaceholderText(_translate("mGui", "Mobile IP", None))
import xz_rc
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
mGui = QtGui.QWidget()
ui = Ui_mGui()
ui.setupUi(mGui)
mGui.show()
sys.exit(app.exec_())
請讓我知道如果你需要更多信息
在'readip()',也沒有必要建立一個新的'Gui'實例。使用'self':'self.lineEdit.text()' – Crispin