【问题标题】:How to move a button Pyside2如何移动按钮 Pyside2
【发布时间】:2019-05-27 08:33:00
【问题描述】:

我不知道为什么,但我无法使用 Pyside2 移动按钮。

我命名了一个按钮 Lina,并尝试使用:

self.__ButtonLina.move(400,400)

但它不起作用,也许我将这行放在代码中的错误位置?

代码如下:(Lina 的按钮在最后一个标签中)

# -*- coding: utf-8 -*-

import sys
from PySide2 import QtCore, QtGui
from PySide2.QtWidgets import *
from PySide2.QtGui import *

# orange Continental rgb(255, 128, 0)

class Dialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self,parent)
        # Les champs
        self.__champTexteNomAuteur = QLineEdit("")
        self.__champTextePrenomAuteur = QLineEdit("")
        self.__champDateNaissanceAuteur = QDateEdit()
        self.__champDateNaissanceAuteur.setCalendarPopup(True)
        self.__champTexteTitreLivre = QLineEdit("")
        self.__champDatePublication = QDateEdit()
        self.__champDatePublication.setCalendarPopup(True)
        self.__ButtonLina = QPushButton("Lina")
        self.__ButtonLina.setMaximumWidth(145)
        self.__ButtonLina.move(400,400)#------<----here is the problem
        self.__ButtonLina.setStyleSheet("background-color: rgb(255, 128, 0);")
        # Les widgets
        self.__widgetAuteur = QWidget()
        self.__widgetLivre = QWidget()
        self.__widget1 = QWidget()
        self.__widget2 = QWidget()
        self.__Tools = QWidget()
        self.__widget1.setStyleSheet("background-color: black;");
        # Les layouts des onglets

        self.__layoutTools = QFormLayout()
        self.__layoutTools.addRow(self.__ButtonLina)
        self.__Tools.setLayout(self.__layoutTools)

        self.__layoutAuteur = QFormLayout()
        self.__widgetAuteur.setLayout(self.__layoutAuteur)
        # La boîte à onglets
        self.__tabWidget = QTabWidget()
        self.__tabWidget.addTab(self.__widgetAuteur, "                  Single Simulation                   ")
        self.__tabWidget.addTab(self.__widgetLivre, "                  Batch Simulation                   ")
        self.__tabWidget.addTab(self.__widget1, "            Vehicule Simulation Tool                ")
        self.__tabWidget.addTab(self.__widget2, "                     Simulation                     ")
        self.__tabWidget.addTab(self.__Tools, "                       Tools                        ")
        # Le layout final
        self.__mainLayout = QVBoxLayout()
        self.__mainLayout.addWidget(self.__tabWidget)
        self.setLayout(self.__mainLayout)
        self.resize(1200,800)
        self.setWindowTitle('VSS Vehicule Simulation Suite')
        self.setStyleSheet("color: black;"
                        "background-color: black");
app = QApplication(sys.argv)
dialog = Dialog()
dialog.exec_()

如果你有任何想法,它会很酷!

【问题讨论】:

    标签: python python-2.7 pyside2


    【解决方案1】:

    布局用于管理它处理的小部件的位置和大小,在您的情况下,__ButtonLina 的位置由 __layoutTools 处理,因此不会应用您手动设置的位置。所以如果你想建立一个固定的位置,你不应该使用布局。

    在这种情况下,解决方案是删除布局并将 __ButtonLina 设置为 __Tools 作为 __ButtonLina 的父级设置为 __Tools

    # ...
    self.__widget1.setStyleSheet("background-color: black;");
    
    # Set as parent of__ButtonLina to __Tools 
    # so that the position of __ButtonLina is relative to __Tools
    self.__ButtonLina.setParent(self.__Tools)
    
    # remove layout
    # Les layouts des onglets
    # self.__layoutTools = QFormLayout()
    # self.__layoutTools.addRow(self.__ButtonLina)
    # self.__ButtonLina.clicked.connect(Lina)
    # self.__Tools.setLayout(self.__layoutTools)
    
    self.__layoutAuteur = QFormLayout()
    # ...
    

    【讨论】:

      猜你喜欢
      • 2019-02-18
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2019-05-22
      • 2015-11-19
      • 2015-11-22
      相关资源
      最近更新 更多