python - Creating a dynamic multiplication application using scales -
i stuck trying make program dynamically multiples 2 numbers scale moved left right.
# module imports import tkinter 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()
you creating value1
twice , not doing value2
, onscale()
wasn't doing anything. must set each scale
widget's command
option desired callback function (onscale
in case).
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())
Comments
Post a Comment