Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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—如何将输入从Entrybox放到Tkinter中的标签上?_Python_Python 3.x_Tkinter_Python 3.3 - Fatal编程技术网

python—如何将输入从Entrybox放到Tkinter中的标签上?

python—如何将输入从Entrybox放到Tkinter中的标签上?,python,python-3.x,tkinter,python-3.3,Python,Python 3.x,Tkinter,Python 3.3,我试图将提交到数据输入框中的信息放入画布的标签中。到目前为止,我在标签上看到的都是这个 PY_VAR# 这是我当前的代码 import tkinter from tkinter import * font = ("Times New Roman", 5) font2 = ("Times New Roman", 10) font3 = ("Times New Roman", 15) def getData(): 1a.get() 2a.get() te

我试图将提交到数据输入框中的信息放入画布的标签中。到目前为止,我在标签上看到的都是这个

PY_VAR#
这是我当前的代码

import tkinter
from tkinter import *
font = ("Times New Roman", 5)
font2 = ("Times New Roman", 10) 
font3 = ("Times New Roman", 15)
def getData():
       1a.get()
       2a.get()
       test()

def enterData():
       global 1a, 2a
       canvas = tkinter.Canvas(root, width=800, height=600)
       box1 = Entry(textvariable = 1a).place(x=100, y=200)
       box2 = Entry(textvariable = 2a).place(x=300, y=200)
       Button(text = "enter data", font = font2, command = getData).place(x=560, y=100)

def test():
        global 1a, 2a
        1a = StringVar()
        2a = StringVar()
        root = tkinter.Tk()
        canvas = tkinter.Canvas(root, width=800, height=600)
        canvas.pack()

        Label(root, text = 1a, font = font3).place(x=70,  y=400)
enterData()
我还试着看看1a和2a分配给什么,总是PY_VAR


有人能看到我的代码有什么问题吗?

您将
标签的
文本
参数指定给变量
1a
,而不是内容。改用这个:

Label(root, text=1a.get())

但是我不得不说,这个代码有点荒谬
1a
2a
甚至不能是变量,
place()
通常是创建GUI的一个坏主意,您正在执行不必要的操作(函数
getData
完全过时),并且您总是在创建新标签。

首先,您不希望每次单击按钮时都创建新的
StringVar
s,因此请将它们的创建移动到顶部,并重命名它们-您不能用数字启动变量(不要忘记在
enterData
函数中将引用更改为
1a
2a
):

其次,
1a.get()
2a.get()
行什么都不做——它们获取条目的内容,但对它们什么都不做。因此,实际上您不需要单独的
getData
test
函数,但为了保留您的结构,我将保留它们:

def getData():
    test()
第三,创建标签时,您不希望将其文本属性设置为
StringVar
,而是希望将textvariable属性设置为
StringVar

Label(root, textvariable = stringVar1a, font = font3).place(x=70,  y=400)

是的,对不起,我的代码乱七八糟,因为我有点糊涂。抱歉。如果您将
textvariable
属性设置为
stringVar1a
,它将持续更新。
Label(root, textvariable = stringVar1a, font = font3).place(x=70,  y=400)