【问题标题】:PyQt- Button ManipulationPyQt - 按钮操作
【发布时间】:2021-01-28 21:29:50
【问题描述】:

我如何以“boxXY”形式使用 Qt 设计器对象名称,其中 X 是行值,Y 值是 python 2D 列表的列值;

import sys,os
from PyQt5 import QtCore, QtGui, uic
rowsColumns=[[],[],[],[],[],[],[],[],[],[]]##Current grid values

allScreens=uic.loadUiType("newSudokuScreens.ui")[0]

class aWindowClass(QtGui.QMainWindow, allScreens):
    def __init__(self, parent=none):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.GPScreen.setVisible(True)
        self.CPScreen.setVisible(False)
        self.DSScreen.setVisible(False)
        self.HTPScreen.setVisible(False)
        self.MMScreen.setVisible(False)
        self.NPSUScreen.setVisible(False)
        self.PSIScreen.setVisible(False)
        self.RUPScreen.setVisible(False)
        self.SPUScreen.setVisible(False)
        self.box00.clicked.connect(self.findRowColumn)
    
    def findRowColumn(self):
        row=#4th character of button name
        column=#5th character of button
        numberWanted=int(input('enter value wanted')) #is part of UI but making row and column value work will help make this work
        rowsColumns[row][column]=numberWanted
    
for i in range(0,9):
    for j in range(0,9):
        rowsColumns[i].append(0) #fill grid with necessary 81 spaces for sudoku
    print(rowsColumns[i])
    
app=QtGui.QApplication(sys.argv)
aWindow=aWindowClass(None)
aWindow.show()
app.exec_()

例如,如果单击 box56 按钮,我想将 box56 作为变量传递,然后在单击框时的过程中,行将设置为 5,列将设置为 6,然后将其附加到二维列表。

提前致谢。

【问题讨论】:

  • 不确定这是否能解决您的问题,但在槽的代码中,您可以使用sender 方法获取对发送点击消息的对象的引用。从对象中,很容易提取标题或任何其他值。
  • @SergeBallesta 你能举个发件人方法的例子吗
  • 当然。但是你应该先把你的代码放在问题本身的文本中,以便我可以复制和粘贴它。
  • 是的,没问题,只需添加代码供您复制和粘贴

标签: python pyqt qt-designer


【解决方案1】:

独立于@eyllanesc 提出的solution,很容易从槽中找到发送信号的对象及其属性。

你可以在这里做:

def findRowColumn(self):
    button_name = self.sender().text()   # gets the text of the button
    row = int(button_name[3]) # 4th character of button name
    column = int(button_name[4]) # 5th character of button
    numberWanted = int(
        input('enter value wanted'))  # is part of UI but making row and column value work will help make this work
    rowsColumns[row][column] = numberWanted

【讨论】:

  • 嗨@Serge Ballesta 感谢您的回答,但不幸的是,给出的答案并不是我所需要的,因为 self.sender().text() 给出了对象内的文本而不是对象的实际名称对象,并且我不能在文本中包含按钮名称,因此是否有类似的或实际上没有方法来传递对象名称,单击按钮以运行程序
  • @SamuelW 也许你想要的是objectName 而不是text。另一个可用的属性是windowTitle
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多