简体   繁体   English

PyQt5:QLineEdit 正在大规模扩展并破坏我的网格布局

[英]PyQt5: QLineEdit is massively expanding and ruining my grid layout

Excuse the probably quite simple question, I'm very new to PyQt5 and am really struggling to understand how it works.请原谅这个可能很简单的问题,我对 PyQt5 很陌生,我真的很难理解它是如何工作的。

I'm trying to make a rough and ready options menu for my program, and as part of that, I'd like to add a few small text boxes for user inputs.我正在尝试为我的程序制作一个粗略且现成的选项菜单,作为其中的一部分,我想为用户输入添加一些小文本框。 I've been using a grid layout system to make things easier for myself, but it seems like the QLineEdit text boxes are trying to expand horizontally and squash everything else.我一直在使用网格布局系统来简化自己的工作,但似乎 QLineEdit 文本框正试图水平扩展并挤压其他所有内容。

What my window looks like without the text box我的 window 看起来像没有文本框

What my window looks like with the text box我的 window 看起来像文本框

I've seen talk about using setHorizontalPolicy, but I don't understand the documentation (and trying to work it out by hand isn't working).我看过有关使用 setHorizontalPolicy 的讨论,但我不理解文档(并且尝试手动解决它是行不通的)。

(Ideally, I'd want my window to look something like this: (理想情况下,我希望我的 window 看起来像这样:

_______________________________________________
|          [title         |      [title]      |
|                         |[label] [text box] |
|                         |[label] [text box] |
|      checkboxes         |[label] [text box] |
|                         |[label] [text box] |
|                         |                   |
|                         |                   |
|                         |  [some buttons]   |
|_________________________|___________________|

) )

Here are the most relevant bits of my code if it helps.如果有帮助,这是我的代码中最相关的部分。

class label_and_qle(QWidget):

def __init__(self, label):
    
    super().__init__()
    self.label = label
    self.initUI()

def initUI(self):
    
    grid = QGridLayout()
    self.setLayout(grid)

    qle = QLineEdit(self)
    qle_label = set_labels()  
    qle.setFixedWidth(80)
    qle_label.set_label_dynamic(self.label)
    # qle.textChanged[str].connect(self.change_options)
    
    grid.addWidget(qle_label, 0, 0, 1, 2)
    grid.addWidget(qle, 0, 2, 1, 1)

class misc_options_gui(QWidget):
    
def __init__(self):
    
    super().__init__()
    self.initUI()
    
def initUI(self):
    
    grid = QGridLayout()
    self.setLayout(grid)
    
    #Set the section title
    lab_title = set_labels()
    lab_title.set_label_title(data_options.misc_options_title)
    grid.addWidget(lab_title, 0, 0)
    
    error_tol = label_and_qle("Error tolerance", data_options.error_tolerance)
    grid.addWidget(error_tol, 5, 0)
    
    #Creat a button to set Folder destination
    set_path =  QPushButton('set folder', self)
    set_path.clicked.connect(self.get_folder)
    grid.addWidget(set_path, 12, 0)
    
    #Create button to launch iMMPY program
    run_button = QPushButton('Run iMMPY script', self)
    run_button.clicked.connect(self.run_iMMPY)
    grid.addWidget(run_button, 14, 0)

def run_iMMPY(self):
    
    if data_options.path == []:
        self.get_folder()
    
    self.showMinimized()
    run_iMMPY()
    self.close()
    
def get_folder(self):
    
    folder = QFileDialog.getExistingDirectory(parent = None, caption = "Select folder")
    data_options.path = folder

class Example(QMainWindow):
'''
This object is the main window of the options menu
'''

def __init__(self):
    
    super().__init__()
    self.initUI()
 
def initUI(self):
    '''
    Initialise main window's UI
    '''
    
    #Create central widget
    self.central_widget = QWidget()               
    self.setCentralWidget(self.central_widget) 
    
    #Define grid layout
    self.grid = QGridLayout()
    self.centralWidget().setLayout(self.grid)
    
    checkboxes = checkboxes_gui(data_options.checkboxes_title, data_options.names_list, data_options.defaults_list)

    misc_options = misc_options_gui()
    

    
    self.grid.addWidget(checkboxes, 0, 0, 1, 3)
    self.grid.addWidget(misc_options, 0, 3, 1, 1)
    checkboxes.resize(500, 750)
    misc_options.resize(250, 750)
    
    #Set window properties
    self.setGeometry(600, 200, 750, 750)
    self.setWindowTitle('Options menu')
    self.show()

Thanks for any help you can give谢谢你提供的所有帮助

The expanding labels are a bit confusing using Qt.使用 Qt 的扩展标签有点混乱。 I've dealt with layout issues a lot.我已经处理了很多布局问题。 A couple things you could try.你可以尝试几件事。 It looks like your label that reads "Error tolerance" is causing the issue.看起来您的 label 显示为“容错”是导致问题的原因。 It is expanding horizontally more than you would like it seems.它的水平扩展比你想象的要多。 If you are using Qt Designer to design the GUI, you can change the "Horizontal Policy" to "Maximum" in the Property editor.如果您使用 Qt Designer 设计 GUI,您可以在属性编辑器中将“水平策略”更改为“最大值”。 This seems counter intuitive but Maximum will make your label expand only as much as the space that the text actually takes up.这似乎与直觉相反,但Maximum 将使您的 label 扩展仅与文本实际占用的空间一样多。 To do this without Qt Designer and just using code, I think this might work:要做到这一点,没有 Qt Designer 并且只使用代码,我认为这可能有效:

    sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Preferred)
    self.label.setSizePolicy(sizePolicy)

replace 'label' with the label Widget that you want to modify.用您要修改的 label 小部件替换“标签”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM