比例函数不将数字输出到变量? [英] Scale function not outputting number to variable?

查看:139
本文介绍了比例函数不将数字输出到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以出于某种原因,tkinter中的比例函数不想输出比例上的数字。我所收到的只有0.0或没有。它似乎是通过GUI来完成GUI和调用功能。写入并运行在python 3.4中。

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

迭代= 30

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

iterscale = Scale(sGui,orient =horizo​​ntal,from_ = 1,to = 1000,variable = itervar)
iterscale.pack()

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

sGui.mainloop()

def saveSettings( ):
全局迭代
迭代= itervar.get()
打印(迭代)

f def doNothing():
pass

def main():
全局根,版本
root = Tk()

menu =菜单(root)
root.config(menu = menu )

fileMenu =菜单(菜单)
menu.add_cascade(label =File,menu = fileMenu)
fileMenu.add_command(label =Quit,command = quit )

benchmarkMenu =菜单(菜单)
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()

我试过函数设置 saveSettings 并且它们工作正常,但是当我通过GUI调用它时,它似乎不起作用。



关于这个问题的任何想法,我唯一的解决方案是使用设置函数和 saveSettings 函数,然后用 os.startfile(etc ...)


<最小修正:改变这个

  itervar = DoubleVar()解决方案

它ervar = DoubleVar(sGui)

因为你有两个根应用程序( root sGui 都是 Tk 的实例) itervar 是第一个创建的,它是 root ,所以当你将它指定为完全不同的应用程序的变量时,tkinter会感到困惑。 >

但我会高度建议使用 Toplevel 实例来保持窗口不变程序:

  sGui = Toplevel(root)
...
#sGui.mainloop()#不再需要这个

尽管如果您希望能够运行设置窗口 / em>您可能会考虑将所有可见窗口设置为 Toplevel s并且隐藏实际的根目录:

 #我不确定是否要将它称为
abs_root = Tk()#但您已经使用roo t
abs_root.withdraw()#隐藏窗口

然后使 root = Toplevel(abs_root)






.get 直接缩放比例:

  iterations = iterscale.get )


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...")

解决方案

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()

这篇关于比例函数不将数字输出到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆