寻求帮助,我的项目输出代码没有运行

问题描述 投票:0回答:1

我面临 PyQt5 代码的问题,当我运行代码时,它没有运行程序。我不知道如何解决它。

这是我的代码

import sys
from PyQt5.QtCore import Qt, QDateTime, QDate
from PyQt5 import uic
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import (
    QCalendarWidget,
    QLineEdit,
    QDateTimeEdit,
    QRadioButton,
    QPlainTextEdit,
    QComboBox,
    QDialog,
    QPushButton,
    QTreeView,
    QTabWidget,
    QApplication,
    QMessageBox,
    QHeaderView,
)

from resources_rc import *


class ToDoInterface(QDialog):
    def __init__(self):
        super(ToDoInterface, self).__init__()
        uic.loadUi("UIoutput/ToDo_interface.ui", self)

        self.widget_connect()

        # Set up buttons
        self.radioButton.setChecked(True)

        # Set the initial state of the DateTimeEdit widget based on the radio button state
        self.set_deadline_visibility()
        self.task_tree()
        self.completed_tree()
        self.pastdue_tree()

    def task_tree(self):
        self.taskView.header().setSectionResizeMode(QHeaderView.ResizeToContents)
        self.taskView.header().setSectionResizeMode(0, QHeaderView.ResizeToContents)
        self.taskView.header().setSectionResizeMode(1, QHeaderView.ResizeToContents)
        self.taskView.header().setSectionResizeMode(2, QHeaderView.ResizeToContents)
        self.taskView.header().setSectionResizeMode(3, QHeaderView.ResizeToContents)
        self.taskView.header().setSections(['Date', 'Task', "Deadline", "Priority"])

    def completed_tree(self):
        self.completedTreeView.header().setSectionResizeMode(QHeaderView.ResizeToContents)
        self.completedTreeView.header().setSectionResizeMode(0, QHeaderView.ResizeToContents)
        self.completedTreeView.header().setSectionResizeMode(1, QHeaderView.ResizeToContents)
        self.completedTreeView.header().setSectionResizeMode(2, QHeaderView.ResizeToContents)
        self.completedTreeView.header().setSectionResizeMode(3, QHeaderView.ResizeToContents)
        self.completedTreeView.header().setSections(['Date', 'Task', "Deadline", "Priority"])

    def pastdue_tree(self):
        self.pastDueTreeView.header().setSectionResizeMode(QHeaderView.ResizeToContents)
        self.pastDueTreeView.header().setSectionResizeMode(0, QHeaderView.ResizeToContents)
        self.pastDueTreeView.header().setSectionResizeMode(1, QHeaderView.ResizeToContents)
        self.pastDueTreeView.header().setSectionResizeMode(2, QHeaderView.ResizeToContents)
        self.pastDueTreeView.header().setSectionResizeMode(3, QHeaderView.ResizeToContents)
        self.pastDueTreeView.header().setSections(['Date', 'Task', "Deadline", "Priority"])

        self.treeView.setAlternatingRowColors(True)
        self.completedTreeView.setAlternatingRowColors(True)
        self.pastDueTreeView.setAlternatingRowColors(True)

    def widget_connect(self):
        # Connect signals and slots
        self.calendarWidget.clicked.connect(self.handle_calendar_click)
        self.radioButton.toggled.connect(self.set_deadline_visibility)
        self.Add_Task.clicked.connect(self.add_toTreeView)
        self.TabWidget.currentChanged.connect(self.tab_changed)
        self.Add_Task.clicked.connect(self.add_toTreeView)
        self.update_button.clicked.connect(self.update_task)
        self.delete_button.clicked.connect(self.delete_task)
        self.complete_button.clicked.connect(self.mark_as_complete)
        self.TabWidget.currentChanged.connect(self.tab_changed)

    def handle_calendar_click(self):
        # Get the selected date from the calendar
        selected_date = self.calendarWidget.selectedDate()

        # Format the date as desired (e.g., month name, date, and year)
        formatted_date = selected_date.toString("MMMM dd, yyyy")

        # Set the formatted date in the text box
        self.lineEdit_2.setText(formatted_date)

    def set_deadline_visibility(self):
        # Set the visibility of the DateTimeEdit widget based on the radio button state
        self.dateTimeEdit_2.setEnabled(self.radioButton.isChecked())

    def add_toTreeView(self):
        Date = self.calendarWidget.selectedDate().toString("MMMM dd, yyyy")
        Task = self.Title_text.text()
        Deadline = self.dateTimeEdit_2.dateTime().toString("MMMM dd, yyyy h:mm AP", "") \
            if self.radioButton.isChecked() else "No Due Date"
        Priority = self.comboBox.currentText()

        # Create a new item for the TreeView
        new_item = QStandardItem()
        new_item.setData(Date, Qt.DisplayRole)
        new_item.setData(Task, Qt.DisplayRole)
        new_item.setData(Deadline, Qt.DisplayRole)
        new_item.setData(Priority, Qt.DisplayRole)

        # Add the item to the TreeView of the current tab
        current_tab_index = self.TabWidget.currentIndex()
        if current_tab_index == 0:
            model = self.treeView.model()
            if model is None:
                model = QStandardItemModel()
                model.setHorizontalHeaderLabels(['Date', 'Task', "Deadline", "Priority"])
                self.treeView.setModel(model)
            model.appendRow(new_item)
        elif current_tab_index == 1:
            model = self.completedTreeView.model()
            if model is None:
                model = QStandardItemModel()
                model.setHorizontalHeaderLabels(['Date', 'Task', "Deadline", "Priority"])
                self.completedTreeView.setModel(model)
            model.appendRow(new_item)
        elif current_tab_index == 2:
            model = self.pastDueTreeView.model()
            if model is None:
                model = QStandardItemModel()
                model.setHorizontalHeaderLabels(['Date', 'Task', "Deadline", "Priority"])
                self.pastDueTreeView.setModel(model)
            model.appendRow(new_item)

    def tab_changed(self, index):
        # Update the current TreeView when the tab is changed
        current_tab_index = self.TabWidget.currentIndex()
        if current_tab_index == 0:
            self.update_current_tree_view(self.treeView)
        elif current_tab_index == 1:
            self.update_current_tree_view(self.completedTreeView)
        elif current_tab_index == 2:
            self.update_current_tree_view(self.pastDueTreeView)

    def update_current_tree_view(self, tree_view):
        # Function to update the current TreeView
        model = tree_view.model()
        selected_indexes = tree_view.selectedIndexes()

        if selected_indexes:
            selected_item = model.itemFromIndex(selected_indexes[0])

            self.Title_text.setText(selected_item.data(Qt.DisplayRole))
            self.calendarWidget.setSelectedDate(QDate.fromString(selected_item.data(Qt.DisplayRole), "MMMM dd, yyyy"))
            self.dateTimeEdit_2.setDateTime(
                QDateTime.fromString(selected_item.data(Qt.DisplayRole), "MMMM dd, yyyy h:mm AP")
            )
            self.comboBox.setCurrentText(selected_item.data(Qt.DisplayRole))

    def update_task(self):
        # Update the selected task in the current TreeView
        current_tab_index = self.TabWidget.currentIndex()
        current_tree_view = self.get_current_tree_view()

        selected_indexes = current_tree_view.selectedIndexes()
        if selected_indexes:
            selected_item = current_tree_view.model().itemFromIndex(selected_indexes[0])

            selected_item.setData(
                self.calendarWidget.selectedDate().toString("MMMM dd, yyyy"), Qt.DisplayRole
            )
            selected_item.setData(self.Title_text.text(), Qt.DisplayRole)
            selected_item.setData(
                self.dateTimeEdit_2.dateTime().toString("MMMM dd, yyyy h:mm AP", ""), Qt.DisplayRole
            )
            selected_item.setData(self.comboBox.currentText(), Qt.DisplayRole)

    def mark_as_complete(self):
        # Move the selected task to the Completed tab
        current_tree_view = self.get_current_tree_view()
        completed_tree_view = self.completedTreeView

        selected_indexes = current_tree_view.selectedIndexes()
        if selected_indexes:
            selected_item = current_tree_view.model().itemFromIndex(selected_indexes[0])
            current_tree_view.model().removeRow(selected_item.row())
            completed_tree_view.model().appendRow(selected_item.clone())

    def delete_task(self):
        # Delete the selected task from the current TreeView
        current_tree_view = self.get_current_tree_view()

        selected_indexes = current_tree_view.selectedIndexes()
        if selected_indexes:
            confirmation = QMessageBox.question(
                self, 'Delete Task', 'Are you sure you want to delete this task?', QMessageBox.Yes | QMessageBox.No,
                QMessageBox.No
            )

            if confirmation == QMessageBox.Yes:
                current_tree_view.model().removeRow(selected_indexes[0].row())

    def get_current_tree_view(self):
        # Return the currently active TreeView based on the selected tab
        current_tab_index = self.TabWidget.currentIndex()
        if current_tab_index == 0:
            return self.treeView
        elif current_tab_index == 1:
            return self.completedTreeView
        elif current_tab_index == 2:
            return self.pastDueTreeView


    if __name__ == '__main__':
        app = QApplication(sys.argv)
    
        # Create an instance of the main window
        window = ToDoInterface()
    
        # Set properties for the main window
        window.setFixedHeight(610)
        window.setFixedWidth(1000)
    
        # Show the main window
        window.show()
    
        sys.exit(app.exec_())

我尝试为我的

QStandardItemModel
添加
TreeView
,但它仍然是一样的。

python user-interface pyqt5 qtreeview
1个回答
0
投票

如果我在手机上没有看到问题,那么您的

if __name__ == '__main__'
语句似乎位于类定义内。您需要通过一次取消所有这些行的缩进来显示该
if
语句。

© www.soinside.com 2019 - 2024. All rights reserved.