gpt4 book ai didi

python - PyQt5 中的 LineEdit 框

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:45 26 4
gpt4 key购买 nike

当我在 lineEdit 框中按 Enter 键时,会同时执行函数 Enter_LineEdit() 和函数 click_Edit()。为什么它执行函数click_Edit()?一定不能!

我希望有人向我解释为什么它会这样工作?

#!/usr/bin/python3.6
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QPushButton,QDialog,QHBoxLayout,QLabel,QWidget,QLineEdit
from PyQt5 import QtGui
from PyQt5 import QtCore


class Window(QMainWindow):

def __init__(self):
super().__init__()
self.setGeometry(100,100,600,400)
self.CreateBtn()
self.show()

def CreateBtn(self):
button = QPushButton("Second Window", self)
button.setGeometry(QtCore.QRect(30,100,200,80))
button.setIconSize(QtCore.QSize(70,70))
button.clicked.connect(self.SecWin)

def SecWin(self):
self.d = SecondWindow()
self.d.Create_SecWin()
self.d.Create_Object()
self.d.Create_Layout()


class SecondWindow(QDialog):

def Create_SecWin(self):
self.setGeometry(600,360,400,100)
self.show()

def Create_Object(self):
self.btnEdit = QPushButton("Edit",self)
self.btnEdit.clicked.connect(self.click_Edit)
self.labelSearch = QLabel("Search:",self)
self.lineEdit = QLineEdit(self)
self.lineEdit.returnPressed.connect(self.enter_LineEdit)

def Create_Layout(self):
hbox1 = QHBoxLayout()
hbox1.addWidget(self.btnEdit)
hbox1.addWidget(self.labelSearch)
hbox1.addWidget(self.lineEdit)
self.setLayout(hbox1)

def click_Edit(self):
print("Philip")

def enter_LineEdit(self):
print("Karl")



App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec_())

最佳答案

如果您查看 autoDefault 的文档QPushButton 的属性:

This property holds whether the push button is an auto default button

If this property is set to true then the push button is an auto default button.

In some GUI styles a default button is drawn with an extra frame around it, up to 3 pixels or more. Qt automatically keeps this space free around auto-default buttons, i.e., auto-default buttons may have a slightly larger size hint.

This property's default is true for buttons that have a QDialog parent; otherwise it defaults to false.

See the default property for details of how default and auto-default interact.

也来自default属性:

[...]

A button with this property set to true (i.e., the dialog's default button,) will automatically be pressed when the user presses enter, with one exception: if an autoDefault button currently has focus, the autoDefault button is pressed. When the dialog has autoDefault buttons but no default button, pressing enter will press either the autoDefault button that currently has focus, or if no button has focus, the next autoDefault button in the focus chain.

[...]

也就是说,在QDialog中按下回车键时,某些QPushButton会被按下,因为所有QPushButton的autoDefault属性都为True,所以解决方案是将其设置为False:

self.btnEdit = QPushButton("Edit", self)
self.btnEdit.setAutoDefault(False)

关于python - PyQt5 中的 LineEdit 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55666624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com