PyQt5 : How to do it? Completed the first command and updated the graphics. and repeat the same until the result is true

huangapple go评论51阅读模式
英文:

PyQt5 : How to do it? Completed the first command and updated the graphics. and repeat the same until the result is true

问题

I'm going to make the blue boxes move to the right one by one. is an anime Until reaching the location of the last box?

有人建议我使用 QTimer,但我仍然不明白如何使用它,以便效果不同,请指导我。

我写的代码,当我们按下“开始”按钮时,它会首先完成所有工作,然后更新图形。
代码:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, 
QLabel, QGridLayout
from PyQt5.QtGui import QFont
import time


class UI_main(QWidget):     

  def __init__(self):
    super().__init__()
    self.Main = QWidget()
    self.Main.setFixedSize(400, 400)

    self.Main.setWindowTitle('PyQt5')

    self.pushStart = QPushButton('Start', self.Main)
    self.pushStart.setGeometry(50, 50, 120, 90)

    self.pushClose = QPushButton('Close', self.Main)
    self.pushClose.setGeometry(250, 250, 100, 100)
    self.ui_Block()
    self.pushStart.clicked.connect(self.push_Start)
    self.pushClose.clicked.connect(self.Main.close)
    self.Main.show()

  def ui_Block(self):
    num = [0, 1, 2, 3]
    self.GridLayout = QGridLayout()
    self.Main.setLayout(self.GridLayout)
    self.numBlock(num)

  def numBlock(self, num):
    self.num = num
    for i in range(len(self.num)):
        self.GridLayout.addWidget(Label_Block(self.num[i]), 0, i)

    self.Main.setLayout(self.GridLayout)    

  def push_Start(self):
    i = 0
    self.num = self.Function(i)

  def Function(self, i):
    self.i = i
    self.i = i + 1
    print(i)
    num, self.i = self.Function2(self.i)
    print("2s")
    time.sleep(2)
    self.numBlock(num)
    self.i = self.i + 1
    num, self.i = self.Function3(self.i)
    print("2s")
    time.sleep(2)
    self.numBlock(num)
    self.i = self.i + 1
    num, self.i = self.Function4(self.i)
    print("2s")
    time.sleep(2)
    self.numBlock(num)

  def Function2(self, i):
    block = [0, 1, 2, 3]
    self.i = i
    print(self.i)
    if self.i == 1:
        block[0] = 1
        block[1] = 0
        block[2] = 2
        block[3] = 3
        self.i = self.i+1
    return block, i

  def Function3(self, i):
    block = [0, 1, 2, 3]
    self.i = i
    print(self.i)
    if self.i == 2:
        block[0] = 1
        block[1] = 2
        block[2] = 0
        block[3] = 3
        self.i = self.i + 1
    return block, i

  def Function4(self, i):
    block = [0, 1, 2, 3]
    self.i = i
    print(self.i)
    if self.i == 3:
        block[0] = 1
        block[1] = 2
        block[2] = 3
        block[3] = 0
        self.i = self.i + 1
    return block, i

class Label_Block(QLabel):
  def __init__(self, block_):
    super().__init()
    self.block_ = block_
    self.setFixedSize(80, 80)
    font = QFont()

    font.setPointSize(30)
    font.setBold(True)
    self.setFont(font)
    if self.block_ == 0:
        self.setStyleSheet("background-color:blue;border-radius:10px;")
        print('block: ', block_)
    elif self.block_ == 1:
        self.setStyleSheet("background-color:red;border-radius:10px;")
        print('block: ', block_)
    elif self.block_ == 2:
        self.setStyleSheet("background-color:green;border-radius:10px;")
        print('block: ', block_)
    elif self.block_ == 3:
        self.setStyleSheet("background-color:yellow;border-radius:10px;")
        print('block: ', block_)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = UI_main()
    sys.exit(app.exec_())
英文:

I'm going to make the blue boxes move to the right one by one. is an anime Until reaching the location of the last box?

enter image description here

Someone advised me to use QTimer but I still don't understand how to use it so the effect is no different, guide me please.

code i wrote when we press the start button It will finish all the work first and then update the graphics.
Code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, 
QLabel, QGridLayout
from PyQt5.QtGui import QFont
import time
class UI_main(QWidget):     
def __init__(self):
super().__init__()
self.Main = QWidget()
self.Main.setFixedSize(400, 400)
self.Main.setWindowTitle('PyQt5')
self.pushStart = QPushButton('Start', self.Main)
self.pushStart.setGeometry(50, 50, 120, 90)
self.pushClose = QPushButton('Close', self.Main)
self.pushClose.setGeometry(250, 250, 100, 100)
self.ui_Block()
self.pushStart.clicked.connect(self.push_Start)
self.pushClose.clicked.connect(self.Main.close)
self.Main.show()
def ui_Block(self):
num = [0, 1, 2, 3]
self.GridLayout = QGridLayout()
self.Main.setLayout(self.GridLayout)
self.numBlock(num)
def numBlock(self, num):
self.num = num
for i in range(len(self.num)):
self.GridLayout.addWidget(Label_Block(self.num[i]), 0, i)
self.Main.setLayout(self.GridLayout)    
def push_Start(self):
i = 0
self.num = self.Function(i)
def Function(self, i):
self.i = i
self.i = i + 1
print(i)
num, self.i = self.Function2(self.i)
print("2s")
time.sleep(2)
self.numBlock(num)
self.i = self.i + 1
num, self.i = self.Function3(self.i)
print("2s")
time.sleep(2)
self.numBlock(num)
self.i = self.i + 1
num, self.i = self.Function4(self.i)
print("2s")
time.sleep(2)
self.numBlock(num)
def Function2(self, i):
block = [0, 1, 2, 3]
self.i = i
print(self.i)
if self.i == 1:
block[0] = 1
block[1] = 0
block[2] = 2
block[3] = 3
self.i = self.i+1
return block, i
def Function3(self, i):
block = [0, 1, 2, 3]
self.i = i
print(self.i)
if self.i == 2:
block[0] = 1
block[1] = 2
block[2] = 0
block[3] = 3
self.i = self.i + 1
return block, i
def Function4(self, i):
block = [0, 1, 2, 3]
self.i = i
print(self.i)
if self.i == 3:
block[0] = 1
block[1] = 2
block[2] = 3
block[3] = 0
self.i = self.i + 1
return block, i
class Label_Block(QLabel):
def __init__(self, block_):
super().__init__()
self.block_ = block_
self.setFixedSize(80, 80)
font = QFont()
font.setPointSize(30)
font.setBold(True)
self.setFont(font)
if self.block_ == 0:
self.setStyleSheet("background-color:blue;border-radius:10px;")
print('block: ', block_)
elif self.block_ == 1:
self.setStyleSheet("background-color:red;border-radius:10px;")
print('block: ', block_)
elif self.block_ == 2:
self.setStyleSheet("background-color:green;border-radius:10px;")
print('block: ', block_)
elif self.block_ == 3:
self.setStyleSheet("background-color:yellow;border-radius:10px;")
print('block: ', block_)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = UI_main()
sys.exit(app.exec_())

答案1

得分: 1

这是一个使用你的代码部分实现的示例,它使用了QTimer,在1秒后发送singleShot信号,以更新QHBoxLayout中标签的位置。有关更多详细信息,请参见内联注释。

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class UI_main(QWidget):

    def __init__(self):
        super().__init__()
        self.setFixedSize(400, 400)
        self.setWindowTitle('PyQt5')
        self.grid_layout = QGridLayout(self)
        self.pushStart = QPushButton('开始', self)
        self.pushStart.setGeometry(50, 50, 120, 90)
        self.pushClose = QPushButton('关闭', self)
        self.pushClose.setGeometry(250, 250, 100, 100)
        self.grid_layout.addWidget(self.pushStart, 0, 0)
        self.grid_layout.addWidget(self.pushClose, 0, 1)
        self.pushStart.clicked.connect(self.push_start)
        self.pushClose.clicked.connect(self.close)
        
        self.hlayout = QHBoxLayout()

        colors = ["红色", "蓝色", "绿色", "黄色"]
        self.labels = []
        for color in colors:
            label = Label_Block(color)
            self.labels.append(label)
            self.hlayout.addWidget(label)

        self.counter = len(self.labels) - 1
        
        self.grid_layout.addLayout(self.hlayout, 1, 0, 1, 2)
        self.timer = QTimer()

    def push_start(self):
        if self.counter == 0:
            self.counter = len(self.labels) - 1
            return
        count = self.hlayout.count()
        item = self.hlayout.takeAt(count-1)
        self.hlayout.insertWidget(0, item.widget())
        self.counter -= 1
        self.timer.singleShot(1000, self.push_start)

class Label_Block(QLabel):

    def __init__(self, color):
        super().__init__()
        self.setFixedSize(80, 80)
        self.setStyleSheet(f"background-color:{color};border-radius:10px;")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui = UI_main()
    ui.show()
    sys.exit(app.exec_())
英文:

Here is an example that uses portions of your code and implements a QTimer that sends singleShot signals after 1 second to update the labels position in a QHBoxLayout... See inline notes for more details.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class UI_main(QWidget):
def __init__(self):
super().__init__() 
# There is no need for a self.Main = QWidget since you are subclassing QWidget
self.setFixedSize(400, 400)
self.setWindowTitle('PyQt5')
self.grid_layout = QGridLayout(self)
self.pushStart = QPushButton('Start', self)
self.pushStart.setGeometry(50, 50, 120, 90)
self.pushClose = QPushButton('Close', self)
self.pushClose.setGeometry(250, 250, 100, 100)
self.grid_layout.addWidget(self.pushStart, 0, 0)
self.grid_layout.addWidget(self.pushClose, 0, 1)
self.pushStart.clicked.connect(self.push_start)
self.pushClose.clicked.connect(self.close)
# I am Using a horizontal layout that only holds the labels
# to make it easier to pop labels off the layout and insert them back in
self.hlayout = QHBoxLayout()   
# iterate the list of colors to create each of the labels
colors = ["red", "blue", "green", "yellow"]  
self.labels = []   # save references to each of the labels.
for color in colors:
label = Label_Block(color)
self.labels.append(label)
self.hlayout.addWidget(label)
# set the number of times you want to move the boxes over by one
self.counter = len(self.labels) - 1   
self.grid_layout.addLayout(self.hlayout,1,0,1,2)
self.timer = QTimer() 
def push_start(self):
# when the counter reaches 0 stop and reset counter
if self.counter == 0:  
self.counter = len(self.labels) - 1
return
# otherwise pop off the last label in the layout and insert it back
# into the layout at the front.  Then reduce the counter by 1.
# and emit a singleShot timer with a 1 second timeout connected to 
# this same method.
count = self.hlayout.count()
item = self.hlayout.takeAt(count-1)
self.hlayout.insertWidget(0, item.widget())
self.counter -= 1
self.timer.singleShot(1000, self.push_start)  # after 1000 ms / 1 sec
class Label_Block(QLabel):
def __init__(self, color):
super().__init__()
self.setFixedSize(80, 80)
self.setStyleSheet(f"background-color:{color};border-radius:10px;")
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = UI_main()
ui.show()
sys.exit(app.exec_())

huangapple
  • 本文由 发表于 2023年2月10日 14:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定