0
私はPython 2.7とPyQT4を使用しています。私は単純な電卓を作っています。私は2つのQLineEdit
を持っていて、追加の結果を出力する関数を持っています。QLineEditの値を別の関数に渡すにはどうすればいいですか?
class Window(QtGui.QMainWindow):
global number_1_text
global number_2_text
def __init__(self):
super(Window, self).__init__()
self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint)
#Main Window Settings
self.setGeometry(50,50,500,300)
self.setWindowTitle("Point of sale !")
self.setWindowIcon(QtGui.QIcon('python.png'))
#Close Action
exitAction = QtGui.QAction(QtGui.QIcon('python.png'),"Close Now !", self)
exitAction.setShortcut("Ctrl+Q")
exitAction.setStatusTip('Leave the Application')
exitAction.triggered.connect(self.close_application)
self.statusBar()
#Menubar
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(exitAction)
# Create textboxs
number_1 = QtGui.QLineEdit(self)
number_1.move(20, 50)
number_1.resize(380,40)
self.number_1_text = number_1.text()
number_2 = QtGui.QLineEdit(self)
number_2.move(20, 100)
number_2.resize(380,40)
self.number_2_text = number_2.text()
#Main Home Proccess
quiteBtn = QtGui.QPushButton("Quite", self)
quiteBtn.resize(100, 50)
quiteBtn.move(50,220)
quiteBtn.clicked.connect(self.close_application)
addBtn = QtGui.QPushButton("Add", self)
addBtn.resize(100, 50)
addBtn.move(150,220)
addBtn.clicked.connect(self.addNumbers)
self.show()
def addNumbers(self):
print self.number_1_text
print self.number_2_text
print "Done"
#Close The Whole Application
def close_application(self):
choice = QtGui.QMessageBox.question(self, 'Exit !', 'Are you sure you wanna exit?',
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No
)
if choice == QtGui.QMessageBox.Yes:
sys.exit()
else:
pass
私はその後、私はaddNumbers
関数内で値を呼び出そうとしましたが、私は空の値を取得し、text()
機能を使用して値を取得しようとしましたが、クラスのグローバル変数に割り当てられています。
は、私はあなたのソリューション を試してみました。しかし、私は トレースバック(最新の呼び出しの最後)だ: ファイル "C:\ Users \ユーザーLilEssamデスクトップの\ pos.py \" を、 0行目9、 クラスウィンドウ(QtGui.QMainWindow): ファイル "C:\ Users \ LilEssam \ Desktop \ pos.py"、行54、ウィンドウ b = float(self.number_2.text()) NameError:名前 'self'は定義されていません –
@AhmedEssam。これは私の例でコードを使用しなかったからです。 'addNumbers'は' Window'クラスのメソッドでなければなりません。 – ekhumoro
ありがとうございました!それは今働いています、ありがとう! –