改进tkinter.ttk代码

时间:2015-08-10 01:10:23

标签: python variables python-3.x tkinter ttk

我是一个相当新手的程序员,并通过咖啡,谷歌和巨大的头发和指甲损失设法写了一个非常混乱的代码。我要求任何人尽可能帮助我简化代码。

from tkinter import ttk
from tkinter import *
from tkinter.ttk import *


one = 0
why = 'Total Number: {}'
no = 0
clack = 0
click = 'Clicks: {}'

s = ttk.Style()
s.theme_use('clam')
s.configure('red.Vertical.TProgressbar', foreground = 'red', background = 'red')
s.configure('green.Vertical.TProgressbar', foreground = 'green', background = 'green')
s.configure('TButton', relief = 'groove')


def iround(x):
    y = round(x) - .5
    return int(y) + (y > 0)

class Application(ttk.Frame):
    def __init__(self, master = None):
        ttk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()        

    def Number(self):
        global one
        cost = 10*(self.w['to'])

        if self.number['text'] == "Make Number go up":
            self.number['text'] = one
        if iround(self.w.get()) == 0:
            one += 1
        else:
            one += iround(self.w.get())
        self.number['text'] = one

        if self.number['text'] >= cost:
            self.buy['state'] = 'normal'
        else:
            self.buy['state'] = 'disabled'
        self.number['text'] >= cost

        self.progress['value'] = one

        if self.number['text'] >= cost:
            self.progress['style'] = 'red.Vertical.TProgressbar'
        else:
            self.progress['style'] = 'green.Vertical.TProgressbar'

    def Buy(self):
        global one
        self.w['to'] += 1
        cost = 10*(self.w['to'])         

        one = self.number['text'] = (one + 10 - cost) 

        self.buy['text'] = ('+1 to slider | Cost: {}'.format(cost))

        if self.number['text'] < (cost):
            self.buy['state'] = 'disabled'

        self.progress['value'] = one
        self.progress['maximum'] += 10

        if self.number['text'] >= cost:
            self.progress['style'] = 'red.Vertical.TProgressbar'
        else:
            self.progress['style'] = 'green.Vertical.TProgressbar'


    def scaleValue(self, event):
        self.v['text'] = 'Slider Bonus + ' + str(iround(self.w.get()))

    def clicks(self, event):
        global click
        global clack
        if self.Clicks['text'] == 'Clicks: 0':
            clack += 1
            self.Clicks['text'] = click.format(clack)
        self.Clicks['text'] = click.format(clack)
        clack += 1

    def NumberVal(self, event):
        global why
        global no
        if self.fun['text'] == "Total Number: 0":
        self.fun['text'] = why.format(no)
        if iround(self.w.get()) == 0:
            no += 1
        else:
            no += iround(self.w.get())
        self.fun['text'] = why.format(no)

    def createWidgets(self):
        self.number = Button(self, text  = 'Make number go up', command = self.Number, width = 20, style = 'TButton')
        self.number.grid(row = 1, column = 1)
        self.number.bind('<ButtonRelease>', self.clicks, add = '+')
        self.number.bind('<ButtonRelease>', self.NumberVal, add = '+')

        self.buy = Button(self, text = '+1 to Slider | Cost: 10', command = self.Buy, width = 20, style = 'TButton')
        self.buy.grid(row = 2, column = 1)
        self.buy.config(state = 'disabled')

        self.v = Label(self, text = 'Slider Bonus + 0', width = 20, anchor = 'center')
        self.v.grid(row = 3, column = 3)

        self.w = Scale(self, from_ = 0, to = 1, orient = 'horizontal')
        self.w.grid(row = 3, column = 1)
        self.w.bind('<Motion>', self.scaleValue)

        self.progress = Progressbar(self, value = 0, orient = 'vertical', maximum = 10, mode = 'determinate')
        self.progress.grid(row = 1, rowspan = 5, column = 2)

        self.Clicks = Label(self, text = 'Clicks: 0', width = 20, anchor = 'center')
        self.Clicks.grid(row = 1, column = 3)

        self.fun = Label(self, text = 'Total Number: 0', width = 20, anchor = 'center')
        self.fun.grid(row = 2, column = 3)

app = Application()
app.master.title('Number')
app.mainloop()

0 个答案:

没有答案