Python Forum
Help on drawing a shape and slider
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help on drawing a shape and slider
#1
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
Reply
#2
Yes its possible.
  1. Select the gui framework you want to use.
  2. Create a gui that takes input from the user.
  3. Using that input draw a shape
  4. Create a slider below it from the values taken from the user.
  5. Create a label that display the value of the slider.
  6. Bind an event to the slider that depending on the results updates the shapes colour.
Reply
#3
Gee. Thanks.
Reply
#4
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?
Reply
#5
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.
Reply
#6
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()
Reply
#7
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
Reply
#8
(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.

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

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 = w1
or remove that line completely
def mod_calc(slider_value) :
    
Reply
#9
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.
Reply
#10
Well try this answer - sorry Tkinter is not my area of expertise ;)

https://stackoverflow.com/questions/3963...der-to-100
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  neeed to activate a custom tk slider janeik 1 830 Oct-09-2023, 09:29 AM
Last Post: deanhystad
  tkinter slider question Nick_tkinter 1 2,487 Feb-22-2021, 01:31 PM
Last Post: deanhystad
  TKinter Slider widget Jemeronimo 1 3,879 Jan-29-2019, 05:32 PM
Last Post: Larz60+
  Making a Matplotlib in Tkinter using a slider Jemeronimo 1 5,661 Dec-05-2018, 08:06 AM
Last Post: Gribouillis
  [Tkinter] Resetting scale value back to 0 after the slider loses focus lwhistler 3 6,940 Mar-07-2017, 10:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020