Help on drawing a shape and slider - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Help on drawing a shape and slider (/thread-20588.html) Pages:
1
2
|
Help on drawing a shape and slider - mnh001 - Aug-20-2019 I'm running Python 3.7.4 on a Windows 7 32-bit laptop. What I'd like to do is write a program where, after taking a numerical input from the user, the program will draw, say, a black-filled shape (eg circle). Then place a slider beneath it with the start and end values taken from calcs done from the input. I'd like a label to the side showing what value the slider is on. Then, when I drag the slider, I'd like the value of the slider to get passed to an expression, the result of which, depending on that result, changes the fill color of the shape. Is something like this possible? And how would I do it? TIA RE: Help on drawing a shape and slider - Yoriz - Aug-20-2019 Yes its possible.
RE: Help on drawing a shape and slider - mnh001 - Aug-21-2019 Gee. Thanks. RE: Help on drawing a shape and slider - mnh001 - Aug-21-2019 Well, your ploy worked. I figured it out once I learned there was something called tkinter. It took a bit to discover how to pass the value of the slider to a variable but after that it was ok. A new question now. I have the start value of the slider hard-coded to 3. When setting the resolution of the slider to 10 it resets the start value of the slider (on the canvas) to 0. I want it to stay at 3 (so the progression of the values will be 3, 13, 23, 33, etc). I can't find anything in the docs about that. How do I fix that? RE: Help on drawing a shape and slider - Denni - Aug-21-2019 If you had included a bit of example code I might have been able to answer that question without having even used tkinter however I would assume the the slider and/or whatever you are setting that changes the other value has a property or some such that you can set and/or adjust on the fly you just make sure it that when its being reset that does not reset to something less than 3 -- it might even have a minimum attribute but again I do not know the specific objects you are working with and without some code to look at I cannot even help you with the looking up kind of thing. RE: Help on drawing a shape and slider - mnh001 - Aug-22-2019 Oh, sure. It's not very pretty but here's what I've got. And yes I know I repeated myself with the update_circle def but ... import math import tkinter from tkinter import * def update_circle() : w.create_oval(50,50,150,150, fill = "blue") return def mod_calc(w1) : slider_value = scale.get() remainder = number % slider_value if remainder == 0 : update_circle() print('Factor: ', slider_value) return number = int(input('Input number: ')) square_root = int(math.sqrt(number)) master = Tk() w = Canvas(master) w.pack() w.create_oval(50,50,150,150, fill = "red") w1 = Label(master, text = "Input Number: " + str(number), font = "Arial 16") w1.pack() w2 = Label(master, text = "Square root: " + str(square_root), font = "Arial 16") w2.pack() scale = Scale(master, from_=3, to=square_root, length = 500, orient=HORIZONTAL, resolution = 10, command = mod_calc) scale.pack() mainloop() RE: Help on drawing a shape and slider - Denni - Aug-22-2019 Okay perhaps I am not understanding something here but I have a basic issue with this as it appears to be a circular reference and even if it is not it is very confusing code. scale = Scale(... command = mod_calc) def mod_calc(w1) : slider_value = scale.get()It appears your mod_calc is defined using scale and your scale is defined using mod_calc begging the question -- which comes first the chicken or the egg RE: Help on drawing a shape and slider - Yoriz - Aug-22-2019 (Aug-22-2019, 03:20 PM)Denni Wrote: Okay perhaps I am not understanding something here but I have a basic issue with this as it appears to be a circular reference and even if it is not it is very confusing code. mod_calc is an event handler function, it is called when the slider value changes.scale has a reference to the scale widget, it is not a function, calling scale.get() is just using its get method.it is not actually necessary to call scale.get() because the event passes the sliders value to the event handler function.def mod_calc(w1) : slider_value = scale.get()could be changed to def mod_calc(w1) : slider_value = w1or remove that line completely def mod_calc(slider_value) : RE: Help on drawing a shape and slider - mnh001 - Aug-22-2019 Ah, yes I see. Pass the value as the argument of the function. def mod_calc(slider_value) :It works. Unfortunately it doesn't fix the problem with the scale start value changing by itself to 0 when I set the resolution to 10. Still haven't found anything in the docs about it. RE: Help on drawing a shape and slider - Denni - Aug-22-2019 Well try this answer - sorry Tkinter is not my area of expertise ;) https://stackoverflow.com/questions/3963329/how-can-i-set-the-default-value-of-my-tkinter-scale-widget-slider-to-100 |