簡體   English   中英

在 tkinter 中設置日歷時遇到問題

[英]having trouble in setting up a calendar in tkinter

我有一個小的 ui 程序,我需要在其中顯示日歷或日期選擇器。 (注意:用戶名和密碼是 root ”)** 我試過這個代碼:

from Tkinter import *
from PIL import Image, ImageTk
import ttkcalendar
class App():
    def __init__(self):
        pass

    root = Tk()
    root.configure(bg='black')
    root.attributes('-alpha', 0.0)


    def mainWindow(self):


        self.root.geometry('{}x{}'.format(600,400))
        self.root.attributes('-alpha', 1)
        self.root.configure(bg='#404040')
        self.ttkcal = ttkcalendar.Calendar(self.root,firstweekday=calendar.SUNDAY)
        self.ttkcal.pack()
        self.root.wm_title("time sheet management system")

        # Create the toolbar as a frame
        self.toolbar = Frame(self.root, borderwidth=1, relief='raised')
        self.toolbar.configure( bg = '#838383')



        # Load all the images first as PNGs and use ImageTk to convert
        # them to usable Tkinter images.
        self.img1 = Image.open('NewIcon.png')
        self.useImg1 = ImageTk.PhotoImage(self.img1)
        self.img2 = Image.open('LoadIcon.png')
        self.useImg2 = ImageTk.PhotoImage(self.img2)
        self.img3 = Image.open('SaveIcon.png')
        self.useImg3 = ImageTk.PhotoImage(self.img3)


       # Set up all the buttons for use on the toolbars.
        newBtn = Button(self.toolbar, image=self.useImg1, command=self.callback)
        newBtn.pack(side=LEFT, fill=X)
        loadBtn = Button(self.toolbar, image=self.useImg2, command=self.callback)
        loadBtn.pack(side=LEFT, fill=X)
        saveBtn = Button(self.toolbar, image=self.useImg3, command=self.callback)
        saveBtn.pack(side=LEFT, fill=X)


        # Add the toolbar
        self.toolbar.pack(side=TOP, fill=X)
        """
        #create a frame
        self.infoArea= Frame(self.root, borderwidth=2,height=40,width=100, relief='raised',bg='red')"""
        self.root.mainloop()


        """
        # Set up a Text box and scroll bar.
        self.scrollbar = Scrollbar(self.root)
        self.scrollbar.pack(side=RIGHT, fill=Y)

        self.text = Text(self.root)
        self.text.pack()

        self.text.config(yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.text.yview)"""


    def  loginClick(self):
         self.userid = self.txt1.get()
         self.password = self.txt2.get()
         print self.userid,self.password
         if self.password == 'root' and self.userid == 'root':
             self.wi.destroy()
             self.mainWindow()



    def callback(self):
        print "A button was pressed"


    def login(self):
        self.wi =  Toplevel (self.root)
        #self.wi.geometry('{}x{}'.format(200,200))
        self.wi.configure(bg='#404040')
        self.wi.overrideredirect(1)
        self.wi.update_idletasks()
        self.w = self.wi.winfo_screenwidth()
        self.h = self.wi.winfo_screenheight()
        self.size = tuple(int(_) for _ in self.wi.geometry().split('+')[0].split('x'))
        self.x = self.w/2 - self.size[0]/2
        self.y = self.h/2 - self.size[1]/2
        self.wi.geometry("%dx%d+%d+%d" % (self.size + (self.x, self.y)))
        self.wi.geometry('{}x{}'.format(400,200))
        self.frame1 = Frame ( self.wi,bg='#404040' )
        self.frame1.pack(side=BOTTOM,fill=X)

        self.butt1 = Button(self.frame1,text= "Cancel" , bg = '#838383',fg='white',command = self.wi.destroy)
        self.butt1.pack(side=RIGHT,padx=1)

        self.butt2 = Button(self.frame1,text= "Login" ,bg = '#838383',fg='white',command = self.loginClick)
        self.butt2.pack(side=RIGHT)

        """self.frame2 = Frame ( self.wi,bg='green' )
        self.frame2.pack(side=BOTTOM,fill=BOTH)"""
        self.lab1 = Label (self.wi,text='UserID',bg='#404040',fg='white')
        #self.lab1.pack(side=LEFT,padx=60,pady=20)
        self.lab1.place(x=60,y=20)

        self.lab2 = Label (self.wi,text='Password',bg='#404040',fg='white')
        #self.lab1.pack(side=LEFT,padx=60,pady=20)
        self.lab2.place(x=60,y=60)

        self.txt1 = Entry( self.wi)
        self.txt1.place(x=140,y=20)

        self.txt2 = Entry( self.wi,show='*')
        self.txt2.place(x=140,y=60)

        """
        self.label = Label(self.wi,text="UserID",bg='#838383',fg='white')
        self.label.place("""

        self.wi.mainloop()



if __name__ == "__main__":
    a = App()
    #a.root.mainloop()
    a.login()

但它給出了“未定義名稱日歷”的錯誤。 我該如何解決這個問題? 或者有沒有其他方法可以在 tkinter 中實現日歷或日期選擇器。

代碼正在使用calendar module ,但沒有導入模塊。

self.ttkcal = ttkcalendar.Calendar(self.root, firstweekday=calendar.SUNDAY)
                                                           ^^^^^^^^^^^^^^^

只需添加以下import語句即可解決問題。

import calendar

暫無
暫無

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

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