ปัญหากับฟังก์ชัน duplication และโกลบอลตัวแปรใน PySide6 แอ๊ป,ฟังก์ชั่นที่จะพิมพ์สรุปรายวันและเดือนคือ duplicated ตอนปุ่มกด

0

คำถาม

ฉันเป็นเชิดแพลตตินั่มทั้งหมดไปใช่และฉันรู้ว่ามีบางอย่างที่ฉันคิดถึงแต่ฉันไม่รู้ว่าสิ่งที่ฉันต้องเป็น PySide6 โปรแกรและฉันสร้างฟังก์ชันที่จะสร้างปฏิทินอยู่ใน QTableWidget ปฏิทินโดยใช้ศูนย์ควบคุม kde ในโมดูลภาษาไพธอน ทุกคนทำงานสบายดีแต่ปัญหามาตอนที่ฉันพยายามที่จะเพิ่มนำร่องปุ่มจะหาต่อไปแล้วเดือนก่อน: นี่คือของฉันฟังก์ชัน:

import sys
import os
import platform
import datetime as dt
import time
import calendar

from PySide6 import *
from PySide6 import QtGui
from PySide6 import QtWidgets
from PySide6 import QtCore
from PySide6.QtGui import QColor

from functools import partial

yy = int(dt.datetime.now().strftime("%Y"))
mm = int(dt.datetime.now().strftime("%m"))

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        global widgets
        widgets = self.ui

        # Calender generator
        self.calender_gen(mm,yy)



    def calender_gen(self, mm_g, yy_g):
        # Creat table rows and columns
        widgets.tableWidget_3.setRowCount(5)
        widgets.tableWidget_3.setColumnCount(7)

        # Table header labels
        week_list = ["Sat","Sun","Mon","Tue","Wed","Thu","Fri"]
        widgets.tableWidget_3.setHorizontalHeaderLabels(week_list)

        # Start inserting days of the month into the table
        row = 0
        col = 0
        for week in calendar.monthcalendar(yy_g,mm_g):
            for day in week:
                if day == 0:
                    widgets.tableWidget_3.setItem(row,col,QTableWidgetItem(" "))
                else:
                    widgets.tableWidget_3.setItem(row,col,QTableWidgetItem(str(day)))
                col += 1
            row += 1
            col = 0
        print(mm_g,yy_g)
        # Connect Buttons to function
        widgets.pushButton_3.clicked.connect(partial(self.next_calendar_butt,mm_g,yy_g))
        widgets.pushButton_2.clicked.connect(partial(self.prev_calendar_butt,mm_g,yy_g))

    def next_calendar_butt(self,mm_new, yy_new):
        mm_new += 1
        if mm_new > 12:
            mm_new = 1
            yy_new += 1

        widgets.tableWidget_3.setRowCount(0)
        widgets.tableWidget_3.setColumnCount(0)
        self.calender_gen(mm_new,yy_new)

    def prev_calendar_butt(self,mm_g_new,yy_g_new):
        mm_g_new -= 1
        if mm_g_new == 0:
            mm_g_new = 12
            yy_g_new -= 1

        widgets.tableWidget_3.setRowCount(0)
        widgets.tableWidget_3.setColumnCount(0)
        self.calender_gen(mm_g_new,yy_g_new)

ตอนที่ผมทำแอ๊ปปฏิทินแสดงอยู่ในโต๊ะเหมือนในภาพ ของภาพของส่วนติดต่อผู้ใช้แบบโต๊ะ

คอนโซลส่งออก ของคอนโซลรอยนิ้วมือ 112021

ตอนที่ฉันคลิกบน pushButton_3 สำหรับครั้งแรกคลิกมันทำงานตามปกติและรอยนิ้วมือ'122021'ในคอนโซล คอนโซลรอยนิ้วมือ 122021 แต่หลังจากที่ผมคลิกบนปุ่มเดียวกันอีกครั้งมันเริ่มจาก duplication:คอนโซลรอยนิ้วมือ'12202112022' คอนโซลรอยนิ้วมือ 12202112022 ถ้าฉัคลิกอีกครั้งมันรอยนิ้วมือ'12 2021 1 2022 1 2022 2 2022' เหมือนในภาพ ป้องภาพรายละเอียดที่นี่ กับทุกคลิกมันทำซ้ำมากกว่าที่มันควรจะพิมพ์เท่านั้นคนแถลงการณ์ผมก็ไม่ต่างกันหรอกe'22022'

ฉันพยายามที่จะย้ายไปที่ด้านล่างบรรทัดออกมาจาก calendar_gen ฟังก์ชัน()แต่ฉัน coudn ไม่องไปส่งค่าพารามิเตอร์แม้แต่หลังจากประกาศว่ากองโกลบอลตัวแปรและกำหนดพวกเขาไปที่พารามิเตอร์:

widgets.pushButton_3.clicked.connect(partial(self.next_calendar_butt,mm_g,yy_g))
widgets.pushButton_2.clicked.connect(partial(self.prev_calendar_butt,mm_g,yy_g))

ฉันพยายามจะทำอย่างนี้: ใน calendar_gen ฟังก์ชัน()ฉันประกาศโกลบอลและตัวแปรอบหมายให้พวกเขาต้องฟังก์ชันพารามิเตอร์เพื่อสร้างบางอย่างเหมือนเป็นโกลบอลพารามิเตอร์

global var_mm
global var_yy

var_mm = mm_g
var_yy = yy_g

แล้วในตอ init(ตัว)ฟังก์ชันฉันใส่พวกนั้น 2 บรรทัด:

def __init__(self):
    widgets.pushButton_3.clicked.connect(partial(self.next_calendar_butt,var_mm,var_yy))
    widgets.pushButton_2.clicked.connect(partial(self.prev_calendar_butt,var_mm,var_yy))

แต่มันไม่ได้ทำงานในคอนโซลมันรอยนิ้วมือ'112021'เมื่อฉันวิ่งแอพฯนั้นตอนที่ฉันคลิกบน pushButton_3 มันรอยนิ้วมือ'122021'และเมื่อคลิกอีกครั้งนะมันรอยนิ้วมือ'122021'อีกครั้งและดังนั้นอยู่ เหมือนกับคนอื่นปุ่มมันรอยนิ้วมือ'102021'อีกครั้งและอีกครั้ง

global-variables pyqt pyqt6 pyside6
2021-11-21 22:34:07
1

คำตอบที่ดีที่สุด

0

Qt สัญญาณการเชื่อมต่อไม่ได้พิเศษเฉพาะเรา....ภาพที่\n(ค่าปริยาย)และสัญญาณสามารถเป็นบางอย่างที่เชื่อมโยงไปที่เดียวกับฟังก์ชั่นมากกว่าหนึ่งครั้ง

ตั้งแต่คุณเชื่อมต่อคน clicked สัญญาณของปุ่มอยู่ calender_gen,everytime นั่นฟังก์ชันเป็นโทรหาคุณจะเพิ่มรอบการ ต่อ การเชื่อมต่อไปพวกนั้นส่งสัญญาณ. ผลลัพธ์ที่ได้นั่นฟังก์ชันที่เกี่ยวข้อจะถูกเรียกอย่างที่หลายครั้งอย่างที่พวกเขาต้องมาเกี่ยวข้องทุกครั้งที่สัญญาณคือ emitted.

อย่างเหมาะสมทางออกคือการเชื่อมต่อฟังก์ชันที่มันจะเปลี่ยนรายเดือนแล้วเก็บเป็นการอ้างอิงไปที่ ปัจจุบัน เดือนสำหรับ"สร้างใหม่"เดือน computation.

ตั้งแต่ฟังก์ชันเป็นเกือบจะเหมือนกัน,และมันคงจะดีถ้ามีเพื่อนกลุ่มพวกเขาอยู่ในเฉพาะตัวและงานเพียงอย่างเดีฟังก์ชั่นและจากนั้นเชื่อมโยงสัญญานแยกกันฟังก์ชันที่จะสุดท้ายแล้วเรียกมันว่าอดีตนักฟังก์ชันกับคนที่เหมาะสมพารามิเตอร์:

class MainWindow(QMainWindow):
    def __init__(self):
        # ...
        self.calender_gen(mm,yy)

        self.pushButton_2.clicked.connect(self.prev_month)
        self.pushButton_3.clicked.connect(self.next_month)

    def calender_gen(self, mm_g, yy_g):
        # Creat table rows and columns
        self.tableWidget_3.setRowCount(5)
        self.tableWidget_3.setColumnCount(7)

        # Table header labels
        week_list = ["Sat","Sun","Mon","Tue","Wed","Thu","Fri"]
        self.tableWidget_3.setHorizontalHeaderLabels(week_list)

        # Start inserting days of the month into the table
        row = 0
        col = 0
        for week in calendar.monthcalendar(yy_g,mm_g):
            for day in week:
                if day == 0:
                    self.tableWidget_3.setItem(row,col,QTableWidgetItem(" "))
                else:
                    self.tableWidget_3.setItem(row,col,QTableWidgetItem(str(day)))
                col += 1
            row += 1
            col = 0

        self.current_month = mm_g
        self.current_year = yy_g

    def prev_month(self):
        self.step_month(-1)

    def next_month(self):
        self.step_month(1)

    def step_month(self, delta):
        mm_new = self.current_month + delta
        mm_year = self.current_year
        if mm_new > 12:
            mm_new = 1
            mm_year += 1
        elif mm_new < 1:
            mm_new = 12
            mm_year -= 1

        self.calender_gen(mm_new, mm_year)

เห็นได้ชัดว่าเหมาะสม implementing QCalendarWidget อาจจะต้องเรียบง่ายอย่างที่มันแล้วให้ส่วนใหญ่ของ functionalities.

2021-11-21 23:56:46

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่