簡體   English   中英

我如何在Python的gui window列表框中顯示課程?

[英]How can i display courses in the listbox of gui window in Python?

我有一個項目是為一所大學制定時間表,當我運行代碼時我沒有任何錯誤,但我遇到了這個問題。 當我在Entry widget上輸入我的excel CSV文件的路徑,然后點擊閱讀顯示,但是課程或過濾后的課程什么都沒有顯示,你能幫我解決這個問題嗎?

import tkinter as tk
import csv
from tkinter import messagebox
from tkinter import ttk

all_courses = []


def read_csv(path):
    # use the csv module to read the contents of the file at the provided path
    with open(path, newline='', encoding='utf-8') as csvfile:
        data = csv.reader(csvfile)
        all_courses.extend([row for row in data])
        display_courses()

def filter_courses(all_courses, year, code):
    # filter the courses based on the selected year and/or code
    filtered_courses = []
    for course in all_courses:
        if (year in course[1] or year == "") and (code in course[0] or code == ""):
            filtered_courses.append(course)
    return filtered_courses


def add_course(course, timetable=None):
    # add the selected course to the timetable
    timetable.append(course)
    print("Added course:", course)


def remove_course(course, timetable=None):
    # remove the selected course from the timetable
    timetable.remove(course)
    print("Removed course:", course)


def clear_timetable(timetable=None):
    # clear the timetable
    timetable.clear()
    print("Timetable cleared.")


def save_timetable(timetable=None):
    # use the csv module to write the timetable to a new file
    with open("timetable.csv", "w", newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerows(timetable)
    print("Timetable saved.")


def display_courses(all_courses=None):
    filtered_courses = filter_courses(all_courses, year_var.get(), code_var.get())
    courses_list.delete(0, tk.END)
    for course in filtered_courses:
        courses_list.insert(tk.END, course)



# add a warning message for when the user doesn't choose a file path
def display_warning():
    if not file_path_entry.get():
        messagebox.showwarning("Warning", "Please select a file path first")

        display_button.config(command=lambda: [display_courses(all_courses), display_warning()])


# create a GUI window
root = tk.Tk()
root.title("Timetable Tool")

# create an Entry widget to accept the input csv file path
file_path_label = tk.Label(root, text="Input File Path:")
file_path_label.pack()
file_path_entry = tk.Entry(root,)
file_path_entry.pack()

file_path_entry.pack()

# create a button to read the contents of the input file
read_button = tk.Button(root, text="Read", command=lambda: read_csv(file_path_entry.get()))
read_button.pack()

# create a button to display the filtered courses
display_button = tk.Button(root, text="Display", command=lambda: display_courses(all_courses))
display_button.pack()

# create a ComboBox widget to allow the user to select the year and code
year_label = tk.Label(root, text="Year:")
year_label.pack()
year_var = tk.StringVar()
year_var.set("")
year_dropdown = tk.OptionMenu(root, year_var, "1", "2", "3", "4","5")
year_dropdown.pack()

code_label = tk.Label(root, text="Code:")
code_label.pack()
code_var = tk.StringVar()
code_var.set("")
code_dropdown = tk.OptionMenu(root, code_var,"CHI" ,"CS","ECE","ECON","EE", "EECS","ENGR","FRE","IE","ISE","LIFE",
                              "MATH", "MGT", "UNI")
code_dropdown.pack()

# create a Label widget for the "Courses" box
courses_label = tk.Label(root, text="Courses:")
courses_label.pack()

# create a Listbox widget to display the filtered courses
courses_list = tk.Listbox(root)
courses_list.pack()

# create a Label widget for the "Selected Courses" box
selected_courses_label = tk.Label(root, text="Selected Courses:")
selected_courses_label.pack()

# create a Listbox widget to display the selected courses
selected_courses_list = tk.Listbox(root)
selected_courses_list.pack()


# create buttons to add and remove courses from the timetable
add_button = tk.Button(root, text="Add", command=lambda: add_course(courses_list.get(courses_list.curselection())))
add_button.pack()
remove_button = tk.Button(root, text="Remove", command=lambda: remove_course(courses_list.get(courses_list.curselection(
))))
remove_button.pack()

# create a button to clear the timetable
clear_button = tk.Button(root, text="Clear", command=clear_timetable)
clear_button.pack()

# create a button to save the timetable to a new file
save_button = tk.Button(root, text="Save", command=save_timetable)
save_button.pack()

s = ttk.Style()
s.theme_use("clam")
courses_list.configure(background='yellow')
selected_courses_list.configure(background='blue')

# run the GUI
root.mainloop()

我試圖激活這個 function 但沒有成功。

read_csv
def read_csv(path):

您在read_csv()中調用了不帶參數的display_courses() ) ,因此display_courses中的all_courses設置為None ,這將在執行filter_courses(all_courses, ...)時引發異常。

all_courses傳遞給read_csv()中的display_courses() ) :

def read_csv(path):
    # use the csv module to read the contents of the file at the provided path
    with open(path, newline='', encoding='utf-8') as csvfile:
        data = csv.reader(csvfile)
        all_courses.extend([row for row in data])
        display_courses(all_courses)

或者將all_courses參數的默認值設置為all_courses而不是display_courses() function 的None

def display_courses(all_courses=all_courses):
    ...

還建議在兩個OptionMenu中添加空字符串 "" 選項,以便您可以重置過濾器:

...
year_dropdown = tk.OptionMenu(root, year_var, "", "1", "2", "3", "4","5")
...
code_dropdown = tk.OptionMenu(root, code_var, "", "CHI" ,"CS","ECE","ECON","EE", "EECS","ENGR","FRE","IE","ISE","LIFE",
                              "MATH", "MGT", "UNI")
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM