Intro. to Programming : Fall 2017 -Online

Csci 1020 September 5, 2017 Program Assignment 3

Write a GUI which uses at least 2 sliders to address a problem of your own invention.

The example s3.py shows how 2 sliders are used to calculate the area of a rectangle, but use your own problem!

Note in s3.py that there is a new function S_move() defined which is passed a number from the slider, S1 or S2, prints the new number in E4, then calls the calculate() function to compute the area and show the result in E3.

Note also that there is no calculate button! The same calculate() function is used, but every time a slider is moved, S_move() is called, and S_move() calls calculate().

You should enhance the sliders (Scale) by making modifications. The slider can be positioned vertically or horizontally. You can change the colors of the background and font. You can lengthen the slider, change tick marks, and other things. Here is one online reference: http://effbot.org/tkinterbook/scale.htm

You can find many other tweaking examples for Scale (Sliders) online by searching on the keywords "tkinter scale".

As usual, you can make additional enhancements to your GUI, and you should ensure that your problem is different in some significant way from the area=width*length example used in the sample codes.

For some examples of different problems:

sample: s3.py

# simple Tkinter GUI - Wetherbee - September 5, 2017 : sampl3.py
# This program calculates the area of a rectangle, uses sliders

from Tkinter import *
# -------------------------------------------------------------
W = Tk() # create the window where everything is placed

# create and place a quit button
def quitter():  # our quit function to call by pressing B_quit
   W.destroy()
   sys.exit()
   
B_quit = Button(W,text='Quit',command=quitter,bg="red") # create a button
B_quit.grid(row=0,column=0)# place Button at 0,0

# create and place a calculate button
def calculate():              # function definition
   length = float( v1.get() ) # get the length v1 in the E1 Entry box, convert to a float
   width  = float( v2.get() ) # get the width v2 in the E2 Entry box, convert to a float
   area = length * width      # calculate area
   v3.set( str(area) )        # place area value in the E3 Entry box using v3

# entry for the length
L1 = Label(W,text="Length")    # create a Label
L1.grid(row=1,column=0)        # place label at 1,0

def S_move(in_val):      # definition
   str_display = "You changed a value to " + str(in_val)
   v4.set( str_display ) # display the slider value
   calculate()           # call function calculate

v1 = DoubleVar()               # entry str variable
S1 = Scale(W, variable=v1,from_=10.0,to=100.0,tickinterval=10.0,command=S_move) # slider 1
S1.grid(row=1,column=1)        # place Entry at 1,1

# entry for the width
L2 = Label(W,text="Width")     # create a Label
L2.grid(row=2,column=0)        # place label at 2,0

v2 = DoubleVar()               # entry str variable
S2 = Scale(W, variable=v2,from_=10.0,to=100.0,tickinterval=10.0,command=S_move) # slider 2
S2.grid(row=2,column=1)        # place Entry at 2,1

# entry for the area
L3 = Label(W,text="Area")      # create a Label
L3.grid(row=3,column=0)        # place label at 3,0
v3 = StringVar()               # entry str variable
E3 = Entry(W, textvariable=v3) # create an Entry box
E3.grid(row=3,column=1)        # place Entry at 3,1

# extra entry below, a comment on the value selected
v4 = StringVar()
E4 = Entry(W,textvariable=v4)
E4.grid(row=4,column=0,columnspan=2)

# --------------------------------------------------------------
mainloop() # hangs here forever, or until Quit button is pressed



Content is neither approved nor reviewed by FDLTCC.