<p>也许我没明白你的意思。对我来说很管用,也许你忘了打印:</p>
<pre><code>[...]
root.myframe = myFrame(root)
#this print myStringVar content even when the value doesn't change (each time you call it):
print("main:", root.myStringVar.get()) #<-- this is the missing print, i guess
def foo(*args): #custom useless function
"""Prints "hello" each time OptionMenu selection change"""
print("foo: hello")
def showargs(*args): #show params, useless too
"""Prints args content each time OptionMenu change"""
print("showargs:", args)
def showcontent(*args): #finally this print myStringVar content
"""Prints myStringVar content each time OptionMenu change"""
print("showcontent: ",root.myStringVar.get())
root.myStringVar.trace('w', foo)
root.myStringVar.trace('w', showargs)
root.myStringVar.trace('w', showcontent)
#output:
#main: hello
#after user select "1" from OptionMenu:
#showcontent: 1
#showargs: ('PY_VAR0', '', 'w')
#foo: hello
</code></pre>
<p>我添加了三种调试方法来测试myStringVar内容:</p>
<ul>
<li><p>foo是无用的,但每次用户更改OptionMenu值时都会触发它,</p></li>
<li><p>showargs显示*args参数内容,</p></li>
<li><p>showcontent显示myStringVar内容。</p></li>
<li><p>如果因为在第19行显示结果而陷入困境,则会忘记将其打印到控制台或以某种方式显示给gui(我添加了一个打印),这样就可以工作了。</p></li>
</ul>
<p>所以您可以传递myStringVar并获取其内容和/或显示其任何更改。</p>