擅长:python、mysql、java
<p>您必须在<code>scaleevent</code>中使用<code>global</code>,因为您试图将值赋给<code>variable</code>。如果没有<code>global</code>,它将<code>v</code>分配给本地{<cd3>},那么它就不存在于{<cd7>}</p>
<pre><code>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()
</code></pre>
<p>至于第二个版本,你用<code>var = Widget(...).grid()</code>出错了</p>
<p>它将<code>None</code>分配给<code>var</code>,因为<code>grid()/pack()/place()</code>返回{<cd9>}。<br/>
你必须分两行来完成:</p>
^{pr2}$
<p>代码</p>
<pre><code>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()
</code></pre>