【发布时间】:2019-06-12 22:58:50
【问题描述】:
这是我的示例程序,在此我有两行编辑,我想使用键盘设置文本。我在两行编辑中得到相同的文本,任何人都可以帮助我如何关注特定选定的内容行编辑。如果我选择了 cash_received 行编辑,我的文本将设置为该对象。提前谢谢你。 下面是我的代码:
import sys
from PyQt4 import QtGui,QtCore
from functools import partial
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.Vbox = QtGui.QGridLayout()
hbox = QtGui.QHBoxLayout(spacing = 0)
hbox.setContentsMargins(0, 0, 0, 0)
cash_btn = QtGui.QPushButton("cash")
card_btn = QtGui.QPushButton("Card")
cash_btn.clicked.connect(self.cash_card_payment)
wallet_btn = QtGui.QPushButton("wallet")
hbox.addWidget(cash_btn)
hbox.addWidget(card_btn)
hbox.addWidget(wallet_btn)
self.Vbox.addLayout(hbox,0,0)
grid = QtGui.QGridLayout()
self.Vbox.addLayout(grid,3,0)
self.setLayout(self.Vbox)
names = [
'7', '8', '9', '-',
'4', '5', '6', '+',
'1', '2', '3', 'enter',
'0', '', '.']
positions = [(i,j) for i in range(4) for j in range(4)]
for position, name in zip(positions, names):
x, y = position
if name == '':
continue
button = QtGui.QPushButton(name)
button.setFocusPolicy(QtCore.Qt.NoFocus)
button.clicked.connect(partial(self.buttonClicked,name))
button.setMinimumWidth(50)
button.setMinimumHeight(50)
if button.text() == 'enter':
button.setMinimumHeight(110)
grid.addWidget(button, x, y, 2,1)
elif button.text() == '0':
grid.addWidget(button, x, y, 1,2)
else:
grid.addWidget(button, x, y, 1,1)
def cash_card_payment(self):
print "cardssss"
cash_payment_vbox = QtGui.QVBoxLayout()
cash_payment_vbox.setAlignment(QtCore.Qt.AlignCenter)
self.cash_received = QtGui.QLineEdit()
self.cash_tender = QtGui.QLineEdit()
cash_payment_vbox.addWidget(self.cash_received)
cash_payment_vbox.addWidget(self.cash_tender)
self.Vbox.addWidget(self.cash_received,1,0)
self.Vbox.addWidget(self.cash_tender,2,0)
def buttonClicked(self,name):
print name
self.cash_received.setText(name)
#herei want to set the text for
# cash_received objec only
self.cash_tender.setText(name) # here i want to set the text for
# cash_tender objec only how can i focus the one line edit to another
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
ex.setWindowTitle('Calculator')
ex.setGeometry(300, 150, 500,400)
sys.exit(app.exec_())
【问题讨论】:
标签: python python-2.7 pyqt pyqt4