Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过按键获取刻度值?_Python_Tkinter_Get_Slider - Fatal编程技术网

Python 如何通过按键获取刻度值?

Python 如何通过按键获取刻度值?,python,tkinter,get,slider,Python,Tkinter,Get,Slider,我真的尽了我最大的努力自己去寻找解决方案,但是没有。我想有一个滑块的值,然后保存到一个csv文件(这是正常工作),点击一个按钮。唉,在按钮事件期间,我无法获取tkinter.Scale的值。我想知道全局变量是否可以解决我的问题,但我还没有让它们发挥作用。我特别惊讶,因为我可以在更改时打印标尺值的实时流,但无法以有用的方式保存它。如果你能回答我的任何困惑,或者让我知道我的问题是否不清楚,或者无论如何可以更好,我将不胜感激。以下是一些帮助我走到这一步的链接: 以下是我打印最终值的10次尝试: f

我真的尽了我最大的努力自己去寻找解决方案,但是没有。我想有一个滑块的值,然后保存到一个csv文件(这是正常工作),点击一个按钮。唉,在按钮事件期间,我无法获取
tkinter.Scale
的值。我想知道全局变量是否可以解决我的问题,但我还没有让它们发挥作用。我特别惊讶,因为我可以在更改时打印标尺值的实时流,但无法以有用的方式保存它。如果你能回答我的任何困惑,或者让我知道我的问题是否不清楚,或者无论如何可以更好,我将不胜感激。以下是一些帮助我走到这一步的链接:

以下是我打印最终值的10次尝试:

from tkinter import *
root = Tk()

def scaleevent(v):    #Live update of value printed to shell
    print(v)
    variable = v

def savevalue():
    global variable              #This is what I want to work, but doesn't
    for i in range(10):
        print(variable)

scale = Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
button = Button(text="button", command=savevalue).grid(column=1, row=0)

root.mainloop()
下面是我使用
.get()
解决问题的尝试:

(Python 3.5、Windows 10)

编辑:

这是我第一次尝试使用全局变量时遇到的错误:

Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“C:\Users\Me\AppData\Local\Programs\Python35\lib\tkinter\\ uuuuuu init\uuuuuuu.py”,第1550行,在调用中__ 返回self.func(*args) 文件“C:\Users\Me\Documents\programming\tkinter scale question.py”,第15行,保存值 打印(变量) NameError:未定义名称“变量”
这就是我运行第一个代码示例时发生的情况,与我的实际项目类似。谢谢布莱恩·奥克利

您必须在
scaleevent
中使用
global
,因为您试图将值分配给
变量。如果没有
global
,它将
v
分配给本地
变量
,然后它就不存在于
savevalue

from tkinter import *

root = Tk()

def scaleevent(v):
    global variable

    print(v)
    variable = v

def savevalue():
    print(variable)

Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
Button(text="button", command=savevalue).grid(column=1, row=0)

root.mainloop()
至于第二个版本,您在
var=Widget(…).grid()上犯了错误

它将
None
分配给
var
,因为
grid()/pack()/place()
返回
None

您必须分两行进行操作:

var = Widget(...)
var.grid(...)
代码

从tkinter进口*

root = Tk()
variable=0 # only you forgot this 
def scaleevent(v):
    print(v)
    global variable

    variable=v

def savevalue():
    print(variable)

Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
Button(text="button", command=savevalue).grid(column=1, row=0)

root.mainloop()

运行代码时会发生什么?你有错误吗?请发布错误。有更好的方法来完成此任务,但第一个代码示例的一个简单修复方法是将
全局变量
指令从
savevalue
移动到
scaleevent
。在第一个代码段中,需要将
全局变量
声明移动到
scaleevent()
函数,否则它只是一个局部变量,因此在调用
savevalue()
时它永远不会被定义,因为该函数所做的只是尝试读取变量的当前值。它工作了!!比你PM 2Ring和martineau,我很高兴事情就这么简单。我很想知道还有什么其他的方法可以做到这一点,但我很高兴这个方法奏效了。谢谢你,谢谢marineau,看起来确实不错。
from tkinter import *

root = Tk()

def savevalue():
    print(scale.get())
    root.destroy() # you forgot ()

scale = Scale(orient='vertical')
scale.grid(column=0,row=0)

button = Button(text="button", command=savevalue)
button.grid(column=1, row=0)

root.mainloop()
root = Tk()
variable=0 # only you forgot this 
def scaleevent(v):
    print(v)
    global variable

    variable=v

def savevalue():
    print(variable)

Scale(orient='vertical', command=scaleevent).grid(column=0,row=0)
Button(text="button", command=savevalue).grid(column=1, row=0)

root.mainloop()