分页: 1 / 1

学习python, 用tk写了个猜生日的游戏..

发表于 : 2015-01-22 0:59
drongh

代码: 全选

from tkinter import *
import tkinter.messagebox


class BirthdaySet:
    def __init__(self, container, name):
        self.container = container
        self.text = name
        self.day = 0
        self.display()

    def processCheckButton(self):
        if self.v.get() == 1:
            self.day = get_day(self.text)
        else:
            self.day = 0

    def display(self):
        frame = Frame(self.container)
        frame.pack(side=LEFT)
        Label(frame, text=self.text, relief=GROOVE).pack()
        self.v = IntVar()
        Checkbutton(frame, variable=self.v, command=self.processCheckButton).pack()


def get_day(str):
    item = str.split()
    lst = [int(x) for x in item]
    return lst[0]


def processButton():
    day = g0.day + g1.day + g2.day + g3.day + g4.day
    if day == 0:
        tkinter.messagebox.showerror("Error", "Please make a choice!!")
    else:
        tkinter.messagebox.showinfo("Found it", "Your birthday is " + str(day))


window = Tk()
window.title("Birthday Guess")

text = [" 1  3  5  7\n 9 11 13 15\n17 19 21 23\n25 27 29 31",
        " 2  3  6  7\n10 11 14 15\n18 19 22 23\n26 27 29 31",
        " 4  5  6  7\n12 13 14 15\n20 21 22 23\n28 29 30 31",
        " 8  9  10  11\n12 13 14 15\n24 25 26 27\n28 29 30 31",
        "16 17 18 19\n20 21 22 23\n24 25 26 27\n28 29 30 31"]

Label(window, text="Check the button if your birthday is in these sets").pack()
frame = Frame(window)
frame.pack()
g0 = BirthdaySet(frame, text[0])
g1 = BirthdaySet(frame, text[1])
g2 = BirthdaySet(frame, text[2])
g3 = BirthdaySet(frame, text[3])
g4 = BirthdaySet(frame, text[4])
Button(window, text="Guess Birthday", command=processButton).pack()

window.mainloop()

Re: 学习python, 用tk写了个猜生日的游戏..

发表于 : 2015-01-22 1:15
drongh
另外,哪位有这本书的全部习题答案.官网上只有偶数题的答案,还有好多做不出来.
<Python语言程序设计> 作者: Y.Daniel Liang. 机械工业出版社