提问人:Denna 提问时间:11/14/2023 最后编辑:acw1668Denna 更新时间:11/14/2023 访问量:45
无法导入 tkcalendar
Can't import tkcalendar
问:
我是编程世界的新手。实际上,我是一名学徒,目前正在免除实习。 我被要求用 python 编写一个程序来组织来自几家诊所的血液样本。我需要写一个日历,并决定使用tkcalendar。到目前为止,我有一个非常不错的运行代码。直到我尝试导入 tkcalendar。你对如何解决这个问题有什么想法吗?
import tkinter as tk
from tkinter import *
import calendar
import locale
from tkcalendar import DateEntry # Date-Entry-Widget importieren
# Funktion um zum nächsten Jahr zu wechslen
def next_year():
global year
year +=1
if root.winfo_exists():
update_calendar()
# Funktion, zum Aktualisieren des Kalenders
def update_calendar(events):
new_text = calendar.calendar(year)
for event in events:
event_date = event['date']
event_text = event['text']
l1.config(text = new_text)
def add_event(events):
selected_date = cal.get_date()
event_text = entry_event.get()
if selected_date and event_text:
events.append({"date": selected_date, "text": event_text})
# Jahr initialisieren
year = 2023
# Tkinter-Gui
root = Tk()
root.geometry ("500x600")
root.title("Kalender")
label1 = Label(root, text ="Kalender", bg = 'dark grey', font = ('Arial', 17))
label1.grid(row = 1, column = 1)
root.config(background = 'white')
l1 = Label (root, text = "", font=('courier', 12, 'bold'), justify=LEFT)
l1.grid(row = 2, column = 1, padx = 20)
# Button um das Jahr zu wechseln
next_year_button = Button(root, text = 'nächstes Jahr', command = next_year)
next_year_button.grid(row = 3, column = 1, pady = 10)
# das Date-Entry-Widget für die Datumsauswahl nutzen
cal = DateEntry(root, width = 12, background = 'darkblue', foreground = 'white', borderwidth = 2)
cal.grid(row = 7, column = 1, pady = 5)
# Eingabe-Label für Terminbeschreibung implementieren
entry_event_label = Label(root, text = 'Termin: ')
entry_event_label.grid(row = 5, column = 1, pady = 5)
entry_event = Entry(root)
entry_event.grid(row = 6, column = 1, pady = 5)
# Event-Button hinzufügen
add_event_button = Button(root, text= 'Termin hinzufügen', command= add_event)
add_event_button.grid(row = 7, column = 1, pady = 10)
#Initialien des Kalenders anzeigen
update_calendar()
# Eventloop starten
root.mainloop()
这是我写的代码,尝试运行它时出现此错误:
Traceback (most recent call last):
File "C:\Users\Müller-Denise\PycharmProjects\pythonProject\venv\Calendar.py", line 5, in <module>
from tkcalendar import DateEntry # Date-Entry-Widget importieren
ModuleNotFoundError: No module named 'tkcalendar'
我已经尝试在 miniconda 中安装 tkcalendar,但它只是说它找不到名为 tkcalendar 的模块。我还检查了错别字,但找不到。
答:
2赞
조영현
11/14/2023
#1
1 检查 Python 版本
python --version
2 使用 pip 安装 tkcalendar
pip install tkcalendar
3 验证安装
python -m tkcalendar
如果以上所有方法都不起作用,请尝试以下操作。
重新启动 IDE 或终端,或通过 Conda 安装
评论
bash
cmd
PyCharm
python
pip
bash
PyCharm
PyCharm
import sys
print( sys.executable )
/full/path/to/python
bash
/full/path/to/python -m pip install ...