我的TKinter计数器表现不正常

时间:2015-02-13 14:58:38

标签: python python-2.7 tkinter

所以我的计数器出现问题,首先它应该在每次点击一个按钮时计数。当计数器超过26时,应将其设置为零并影响其他计数器。问题是当我通过单击减少将它们全部设置为26然后单击增加它不会表现得应该如此。非常感谢帮助。

#imports everything from tkinter
import Tkinter as tk
from Tkinter import*

class Store1:
    def __init__(self):
        self.variable1 = tk.IntVar()
        self.variable2 = tk.IntVar()
        self.variable3 = tk.IntVar()

    def add1(self):

        counter1 = self.variable1
        counter2 = self.variable2
        counter3 = self.variable3

        counter1.set(counter1.get() + 1)

        if counter1.get() > 26:
            counter1.set(1)
            counter3.set(counter3.get() + 1)
        if counter2.get() > 26:
            counter2.set(1)
            counter3.set(counter3.get() + 1)
        if counter3.get() > 26:
            counter3.set(1)

        return counter1.get(), counter2.get(), counter3.get()

    def add2(self):
        counter1 = self.variable1
        counter2 = self.variable2
        counter3 = self.variable3

        counter2.set(counter2.get() + 1)
        if counter2.get() > 26:
            counter2.set(1)
            counter1.set(counter1.get() + 1)
        if counter3.get() > 26:
            counter3.set(1)
            counter2.set(counter2.get() + 1)
        if counter1.get() > 26:
            counter1.set(1)
            counter3.set(counter3.get() + 1)

        return counter1.get(), counter2.get(), counter3.get()

    def add3(self):
        counter1 = self.variable1
        counter2 = self.variable2
        counter3 = self.variable3

        counter3.set(counter3.get() + 1)
        if counter3.get() > 26:
            counter3.set(1)
            counter2.set(counter2.get() + 1)
        if counter2.get() > 26:
            counter2.set(1)
            counter1.set(counter1.get() + 1)
        if counter1.get() > 26:
            counter1.set(1)
            counter3.set(counter3.get() + 1)

        return counter1.get(), counter2.get(), counter3.get()


    def sub1(self):
        counter1 = self.variable1
        counter1.set(counter1.get() - 1)
        if counter1.get() < 1:
            counter1.set(26)

        return counter1.get()

    def sub2(self):
        counter2 = self.variable2
        counter2.set(counter2.get() - 1)
        if counter2.get() < 1:
            counter2.set(26)

        return counter2.get()

    def sub3(self):
        counter3 = self.variable3
        counter3.set(counter3.get() - 1)
        if counter3.get() < 1:
            counter3.set(26)

        return counter3.get()

class Main(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        counter1 = Store1()
        counter2 = Store1()
        counter3 = Store1()

  def all_counters1():
            counter1.add1()
            counter2.add1()
            counter3.add1()

        def all_counters2():
            counter1.add2()
            counter2.add2()
            counter3.add2()

        def all_counters3():
            counter1.add3()
            counter2.add3()
            counter3.add3()


        tk.Button(self, width=10, text="Increase", command=all_counters1, fg="blue", bg = "white").grid(column=0, row=0, padx=20, pady=(10,0))
        tk.Label(self, width=5, height=2, textvariable=counter1.variable1, bg = "white", font = "Verdana 16 bold").grid(column=0,row=1,padx=20, pady=10)
        tk.Button(self, width=10, text="Decrease",command=lambda: counter1.sub1(), fg="blue", bg = "white").grid(column=0, row=2, padx=20, pady=10)

        tk.Button(self, width=10, text="Increase", command=all_counters2, fg="blue", bg = "white").grid(column=1, row=0, padx=10,pady=(10,0))
        tk.Label(self, width=5, height=2, textvariable=counter2.variable2, bg = "white", font = "Verdana 16 bold").grid(column=1, row=1, padx=10,pady=10)
        tk.Button(self, width=10, text="Decrease", command=lambda: counter2.sub2(), fg="blue", bg = "white").grid(column=1, row=2, padx=10,pady=10)

        tk.Button(self, width=10, text="Increase", command=all_counters3, fg="red", bg = "white").grid(column=2, row=0, padx=10, pady=(10,0))
        tk.Label(self, width=5, height=2, textvariable=counter3.variable3, bg = "white", font = "Verdana 16 bold").grid(column=2, row=1, padx=10,pady=10)
        tk.Button(self, width=10, text="Decrease", command=lambda: counter3.sub3(), fg="red", bg = "white").grid(column=2, row=2, padx=10,pady=10)

root = Main()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

这方面的一个例子至少可以解决我想要的一些问题。

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

from functools import partial

def change_counter(var, amount, number):
   result=var.get()
   result += amount
   if result > 26 or result < 0:
      if number==2:
         print "number 3 is >= 26 or < 0"
      result=0
   var.set(result)

top=tk.Tk()

colors=["blue", "orange", "green"]
for ctr in range(3):
    variable = tk.IntVar()
    tk.Button(top, width=10, text="Increase",
         command=partial(change_counter, variable, +1, ctr),
         fg=colors[ctr], bg="white").grid(column=ctr, row=0,
         padx=20, pady=(10,0))
    tk.Label(top, width=5, height=2, textvariable=variable,
         bg="white", font="Verdana 16 bold").grid(column=ctr,
         row=1,padx=20, pady=10)
    tk.Button(top, width=10, text="Decrease",
          command=partial(change_counter, variable, -1, ctr), fg=colors[ctr],
          bg="white").grid(column=ctr, row=2, padx=20, pady=10)

top.mainloop()