【问题标题】:export tkinter calendar to another tkinter将 tkinter 日历导出到另一个 tkinter
【发布时间】:2014-11-14 15:02:51
【问题描述】:

我有一个名为 wckcalendar.py 的 tkinter 日历文件。我想将它导入到另一个文件中,代码如下。我想在 同一个 tkinter 窗口中显示日历和我的按钮 请让我知道我需要做的更改,以便两者都显示在同一个窗口上!

得到错误:

Traceback (most recent call last):
  File "C:/Python27/qw.py", line 17, in <module>
    app = myproject(None, None)
  File "C:/Python27/qw.py", line 8, in __init__
    self.calendar()
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1826, in __getattr__
    return getattr(self.tk, attr)
AttributeError: calendar

我的代码:

import wckCalendar
from wckCalendar import *
import Tkinter
class myproject(Tkinter.Tk):
    def __init__(self,parent, master):
        Tkinter.Tk.__init__(self)
        self.button2()
        self.calendar()
        win(root, data)
    def button2(self):
        button2 = Tkinter.Button(self, text = "Benny")
        button2.grid(column=1,row=3)

    def win(parent, d):
        win = tk.Toplevel(parent)
        cal = Calendar(win, d)
app = myproject(None, None)
app.mainloop()        

我的 wckcalendar 文件编码:

import calendar
import Tkinter as tk
import datetime

class Data:
    def __init__(self):
        self.day_selected = 0
        self.month_selected = 0
        self.year_selected = 0
        self.day_name = 0

class Calendar:
    def __init__(self, parent, data):
        self.data = data
        self.parent = parent
        self.cal = calendar.TextCalendar(calendar.SUNDAY)
        self.year = 2014
        self.month = 11
        self.wid = []
        self.day_selected = 1
        self.month_selected = self.month
        self.year_selected = self.year
        self.day_name = ''

        self.setup(self.year, self.month)

    def clear(self):
        for w in self.wid[:]:
            w.grid_forget()
            #w.destroy()
            self.wid.remove(w)

    def go_prev(self):
        if self.month > 1:
            self.month -= 1
        else:
            self.month = 12
            self.year -= 1
        #self.selected = (self.month, self.year)
        self.clear()
        self.setup(self.year, self.month)

    def go_next(self):
        if self.month < 12:
            self.month += 1
        else:
            self.month = 1
            self.year += 1

        #self.selected = (self.month, self.year)
        self.clear()
        self.setup(self.year, self.month)

    def selection(self, day, name):
        self.day_selected = day
        self.month_selected = self.month
        self.year_selected = self.year
        self.day_name = name

        self.data.day_selected = day
        self.data.month_selected = self.month
        self.data.year_selected = self.year
        self.data.day_name = name

        #self.selected = day
        self.clear()
        self.setup(self.year, self.month)

    def setup(self, y, m):
        left = tk.Button(self.parent, text='<', command=self.go_prev)
        self.wid.append(left)
        left.grid(row=0, column=1)

        header = tk.Label(self.parent, height=2, text='{}   {}'.format(calendar.month_abbr[m], str(y)))
        self.wid.append(header)
        header.grid(row=0, column=2, columnspan=3)

        right = tk.Button(self.parent, text='>', command=self.go_next)
        self.wid.append(right)
        right.grid(row=0, column=5)

        days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
        for num, name in enumerate(days):
            t = tk.Label(self.parent, text=name[:3])
            self.wid.append(t)
            t.grid(row=1, column=num)

        for w, week in enumerate(self.cal.monthdayscalendar(y, m), 2):
            for d, day in enumerate(week):
                if day:
                    #print(calendar.day_name[day])
                    b = tk.Button(self.parent, width=1, text=day, command=lambda day=day:self.selection(day, calendar.day_name[(day-1) % 7]))
                    self.wid.append(b)
                    b.grid(row=w, column=d)

        sel = tk.Label(self.parent, height=2, text='{} {} {} {}'.format(
            self.day_name, calendar.month_name[self.month_selected], self.day_selected, self.year_selected))
        self.wid.append(sel)
        sel.grid(row=8, column=0, columnspan=7)

        ok = tk.Button(self.parent, width=5, text='OK', command='disabled')
        self.wid.append(ok)
        ok.grid(row=9, column=2, columnspan=3, pady=10)        
def win(parent, d):
    win = tk.Toplevel(parent)
    cal = Calendar(win, d)
data = Data()
root = tk.Tk()
win(root, data)

root.mainloop()

【问题讨论】:

  • 仅仅从定义了一个类的模块导入,该类的实例具有calendar 属性不会神奇地给myproject 一个calendar 属性。尝试将在wckcalendar.py 中运行的代码隐藏在if __name__ == "__main__": 保护之后,并在qw.py 中创建一个新的Calendar 实例。

标签: python import tkinter


【解决方案1】:

我相信您想要的是一个可重用的日历小部件,它是 Frame 的子类。它应该像这样开始

class Calendar(tk.Frame):
    def __init__(self, master, data, **config):
        tk.Frame.__init__(self, master, cnf=config)
        self.data = data
        ...

正如乔恩所说,以

结束
if __name__ == "__main__":
    # all the stuff not needed when importing Calendar into another module>
    def win(parent, d):
        win = tk.Toplevel(parent)
        cal = Calendar(win, d)
    data = Data()
    root = tk.Tk()
    win(root, data)

root.mainloop()

据我所知,Data 类也属于这里,除非您将其概括为接受参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-18
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    相关资源
    最近更新 更多