python - 为什么打印 py_varo 而不是文本框中的值?

标签 python tkinter python-3.3

出于某种原因,当我按下按钮时,将打印 py_varo 而不是文本框中的值。知道为什么吗?

import sys
from tkinter import*

mygui = Tk()
message = StringVar()
mygui.geometry('300x300+982+0')
mygui.title("hey everyone check out my gui")
mylabel1 = Label(mygui, text = 'hello world',fg = 'yellow', bg = 'blue')
mylabel1.grid(row = 0, column = 0)
def button():
    global message
    print (message)
textbox = Entry(mygui, textvariable =message)
textbox.grid(row = 0, column = 2)
mybutton = Button(mygui, text = 'Pointless Button', command = button)
mybutton.grid(row = 0,column = 1)

mygui.mainloop()

最佳答案

出现这种情况是因为您没有获取使用 get() 方法完成的 StringVar 中的值

因此,将您的 button() 函数更改为:

def button():
    global message
    print (message.get())

关于python - 为什么打印 py_varo 而不是文本框中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20659009/

相关文章:

python - 在 Kivy 中使用 jsonstore

python - Jinja2中模板的动态使用

Python - 理解 CSV 模块和 line_num 对象

python - 什么是 PasteDeploy,如果认为 Python 中的 Eggs 消失了,我还需要学习它吗?

python - 平均功能不是平均而是重置

python - Django Admin - 适用于一个整数模型的选择,但不适用于另一个

python - PySide2/QML 在给定根索引处过滤分层数据

python - time.sleep 阻止按下 tkinter.Button

python - 基于另一个条目小部件的内容更新条目小部件

带有简单网络包装器的 python tkinter