0

So for some reason the scale function in tkinter doesn't want to output the number on the scale. All i receive is either 0.0 or nothing. It seems to be to do with the GUI and calling functions through the GUI. Written and run in python 3.4.

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

iterations=30

def settings():
    global itervar, iterscale
    sGui = Tk()
    itervar = DoubleVar()

    iterscale = Scale(sGui, orient="horizontal", from_=1, to=1000, variable=itervar)
    iterscale.pack()

    iterbutton = Button(sGui, text="Save Settings", command=saveSettings)
    iterbutton.pack()

    sGui.mainloop()

def saveSettings():
    global iterations
    iterations = itervar.get()
    print(iterations)

def doNothing():
    pass

def main():
    global root, version
    root= Tk()

    menu = Menu(root)
    root.config(menu=menu)

    fileMenu = Menu(menu)
    menu.add_cascade(label="File", menu=fileMenu)
    fileMenu.add_command(label="Quit", command=quit)

    benchmarkMenu = Menu(menu)
    menu.add_cascade(label="Benchmark", menu=benchmarkMenu)
    benchmarkMenu.add_command(label="Run [All]", command=doNothing)
    benchmarkMenu.add_separator()
    benchmarkMenu.add_command(label="Settings", command=settings)

    root.mainloop()

#Main
main()

I have tried the functions settings and saveSettings on their own and they work fine, but when i call it through the GUI it doesn't seem to work.

Any ideas on the problem, my only solution would be have the settings function and saveSettings function in a different file and then run that file externally with os.startfile("etc...")

7
  • why do you call sGui.mainloop() in the settings function? Commented Apr 18, 2016 at 16:19
  • so that the settings window stays open when run ?!?! Commented Apr 18, 2016 at 16:20
  • 1
    .... oh it is another Tk instance... ok you need to change it to a Toplevel so that it will exist as another window to the main program. Commented Apr 18, 2016 at 16:21
  • topmost just allows the window to stay in front of the main GUI Commented Apr 18, 2016 at 16:22
  • 1
    FWIW, you shouldn't have more than one root Tk() window in a program, and you mustn't have more than one mainloop call. Commented Apr 18, 2016 at 16:28

1 Answer 1

3

Minimal fix: change this

itervar = DoubleVar()

to this:

itervar = DoubleVar(sGui)

Because you have two root applications (root and sGui are both instances of Tk) the implied parent widget for itervar is the first one created, being root so tkinter gets confused when you specify it as the variable for a completely different application.

But I would highly recommend using a Toplevel instance to keep the windows a part of the same program:

sGui = Toplevel(root)
...
#sGui.mainloop() #no longer need this

although if you want to be able to run the setting window without the main one you might consider making all your visible windows Toplevels and make the actual root hidden:

                # I'm not sure if you want to call it this
abs_root = Tk() # but you are already using root
abs_root.withdraw() #hide the window

Then make root = Toplevel(abs_root)


You coud phase out the variable all together by using .geting the scale directly:

iterations = iterscale.get()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.