使用比例创建动态乘法应用程序

时间:2015-05-11 20:52:44

标签: python tkinter

我目前真的很难尝试制作一个动态倍增两个数字的程序,因为比例从左向右移动。

# Module imports
import Tkinter as tk

# Function Definitions
class Application(tk.Frame):

    def __init__(self, parent = None):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.setupUI()
        self.createWidgets()

    def setupUI(self):
        self.parent.title("Multiplication Scale")
        self.grid()
        self.centerWindow()

    def centerWindow(self):
        app_width = 400
        app_height = 250

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        x = (sw - app_width)/2
        y = (sh - app_height)/2
        self.parent.geometry('%dx%d+%d+%d' % (app_width, app_height, x, y))

    def quit_pressed(self):
        self.parent.destroy()

    def createWidgets(self):
        self.value1 = tk.IntVar()
        self.scaleFirstN = tk.Scale(self, from_=0, to=100,tickinterval=10,orient=tk.HORIZONTAL)
        self.scaleFirstN.grid(row=0,column=0,columnspan=5,ipadx=100,padx=40,pady=10,sticky="n")
        self.value1 = tk.IntVar()
        self.scaleSecondN = tk.Scale(self, from_=0, to=100,tickinterval=10, orient=tk.HORIZONTAL)
        self.scaleSecondN.grid(row=1,column=0,columnspan=5,ipadx=100,padx=40,pady=0,sticky="n")
        self.value2 = tk.IntVar()
        self.label1 = tk.Label(self,textvariable=self.value1, text=0,foreground="blue")
        self.label1.grid(row=5,column=0,columnspan=5, ipadx=5, sticky="wes")
        self.label2 = tk.Label(self,textvariable=self.value1, text=0,foreground="blue")
        self.label2.grid(row=1,column=0,columnspan=5, ipadx=5, sticky="we")
        self.label3 = tk.Label(self,textvariable=self.value1, text=0,foreground="blue")
        self.label3.grid(row=1,column=0,columnspan=5, ipadx=5, sticky="we")

    def onScale(self, val):
        value = self.scale.get()
        self.value.set(value)

# Main body
root = tk.Tk()
app = Application(root)
root.mainloop()

1 个答案:

答案 0 :(得分:2)

您创建了value1两次而没有对value2做任何事情,然后onScale()没有做任何事情。您必须将每个Scale窗口小部件的command选项设置为所需的回调函数(在这种情况下为onScale)。

def createWidgets(self):
    self.value1 = tk.IntVar()
    self.scaleFirstN = tk.Scale(self, from_=0, to=100,tickinterval=10,orient=tk.HORIZONTAL, command=self.onScale, var=self.value1)
    self.scaleFirstN.grid(row=0,column=0,columnspan=5,ipadx=100,padx=40,pady=10,sticky="n")
    self.value2 = tk.IntVar()
    self.scaleSecondN = tk.Scale(self, from_=0, to=100,tickinterval=10, orient=tk.HORIZONTAL, command=self.onScale, var=self.value2)
    self.scaleSecondN.grid(row=1,column=0,columnspan=5,ipadx=100,padx=40,pady=0,sticky="n")
    self.label1 = tk.Label(self,textvariable=self.value1, text=0,foreground="blue")
    self.label1.grid(row=5,column=0,columnspan=5, ipadx=5, sticky="wes")
    self.label2 = tk.Label(self,textvariable=self.value2, text=0,foreground="blue")
    self.label2.grid(row=6,column=0,columnspan=5, ipadx=5, sticky="we")
    self.label3 = tk.Label(self, text=0,foreground="blue")
    self.label3.grid(row=7,column=0,columnspan=5, ipadx=5, sticky="we")

def onScale(self, val):
    self.label3.config(text=self.value1.get() * self.value2.get())