计划射击场射箭

时间:2013-11-26 23:50:05

标签: python tkinter

我正在尝试构建一个python tkinter prog来得分射箭现在我遇到的问题是绑定下面按钮的值,以便在点击按钮时将值加在一起&写入文件

from Tkinter import *
import math
class App(object):

    def __init__(self, master, total=0):
        frame = Frame(master)
        frame.pack()

        Label(frame, text='First Arrow').pack()

        bttn0= Button(frame, text="Pk", command=self.addScore24)
        bttn0.pack()
        bttn1= Button(text='k1', command=self.addScore20)
        bttn1.pack()
        bttn2=Button(text='w1', command=self.addScore16)
        bttn2.pack()
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)

        Label(text='Second Arrow').pack()
        frame=Frame(master)
        frame.pack()

        bttn3=Button(text='k2', command=self.addScore14)
        bttn3.pack()
        bttn4=Button(text='w2', command=self.addScore10)
        bttn4.pack()
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)

        Label(text='Third Arrow').pack()
        frame=Frame(master)
        frame.pack()

        bttn5=Button(text='k3', command=self.addScore8)
        bttn5.pack()
        bttn6=Button(text='w3', command=self.addScore4)
        bttn6.pack()
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)

        Label(text='Total').pack()
        frame.__init__()
        bttn = Button(text='score')
        bttn.pack()
        frame=Frame()
        frame.pack()
        Entry(frame).pack()

    def addScore24(self):
        print'1-24 =', str(24)

    def addScore20(self):
        print'1-20 =', str(20)

    def addScore16(self):
        print'1-16 =', str(16)

    def addScore14(self):
        print'2-14 =', str(14)

    def addScore10(self):
        print'2-10 =', str(10)

    def addScore8(self):
        print'3-8 =', str(8)

    def addScore4(self):
        print'3-4 =', str(4)

root = Tk()
root.wm_title('Archery Scores')
root.geometry('200x400')
app = App(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

我向self.score添加值并将其保存到文件(使用add())。我还将self.score添加到Entry

使用load(),您可以从文件中加载以前的self.score

请参阅按钮'w3''k3'的声明,了解如何将lambda功能与add()

一起使用
from Tkinter import *

class App(object):

    def __init__(self, master, total=0):
        frame = Frame(master)
        frame.pack()

        Label(frame, text='First Arrow').pack()

        bttn0= Button(frame, text="Pk", command=self.addScore24)
        bttn0.pack()
        bttn1= Button(text='k1', command=self.addScore20)
        bttn1.pack()
        bttn2=Button(text='w1', command=self.addScore16)
        bttn2.pack()
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)

        Label(text='Second Arrow').pack()
        frame=Frame(master)
        frame.pack()

        bttn3=Button(text='k2', command=self.addScore14)
        bttn3.pack()
        bttn4=Button(text='w2', command=self.addScore10)
        bttn4.pack()
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)

        Label(text='Third Arrow').pack()
        frame=Frame(master)
        frame.pack()

        bttn5=Button(text='k3', command=lamba:self.add(8) )
        bttn5.pack()
        bttn6=Button(text='w3', command=lamba:self.add(4) )
        bttn6.pack()
        separator = Frame(height=3, bd=1, relief=SUNKEN)
        separator.pack(fill=X, padx=5, pady=5)

        Label(text='Total').pack()
        frame.__init__()
        bttn = Button(text='Score')
        bttn.pack()
        frame = Frame()
        frame.pack()

        self.score_str = StringVar()
        self.ent = Entry(frame, textvariable=self.score_str)
        self.ent.pack()

        self.score = 0
        #self.load() # load previous score
        print "current result:", self.score
        self.score_str.set( str(self.score) )

    def addScore24(self):
        self.add(24)

    def addScore20(self):
        self.add(20)

    def addScore16(self):
        self.add(16)

    def addScore14(self):
        self.add(14)

    def addScore10(self):
        self.add(10)

    def addScore8(self):
        self.add(8)

    def addScore4(self):
        self.add(4)

    def add(self, value):
        self.score += value
        print "current result:", self.score
        self.score_str.set( str(self.score) )
        self.save(self.score)

    def save(self, value):
        with file("result.txt", "w") as f :
            f.write( str(value) )

    def load(self):
        try:
            with file("result.txt", "r") as f :
                self.score = int(f.readline())
        except:
            print "problem with file: result.txt"


root = Tk()
root.wm_title('Archery Scores')
root.geometry('200x400')
app = App(root)
root.mainloop()