为什么两个 tkinter 条目使用相同的数字? [英] Why are the two tkinter entries using the same number?

查看:24
本文介绍了为什么两个 tkinter 条目使用相同的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import os
import tkinter
import tkinter.font as tkFont
from tkinter import *

coord1 = "0,0"
coord2 = "0,0"
EQ = "y = mx + b"


def tkinter_window():
    global coord1Entry
    global coord2Entry
    global coord1
    global coord

    tk = Tk()

    tk.title("Math Graph")

    #create
    font1 = tkFont.Font(family="Source Code Pro", size=16)
    font2 = tkFont.Font(family="Source Code Pro", size=10)

    coord1Label = Label(tk, text='X coordinate 1:\n( "x,y", no parentheses )', font=font1)
    coord2Label = Label(tk, text='Y coordinate 2:\n( "x,y", no parentheses )', font=font1)

这是我定义似乎使用相同数字的两个条目的部分:

This is the part where i define the two entries that seem to use the same numbers:

    coord1Entry = Entry(tk, textvariable=coord1)
    coord2Entry = Entry(tk, textvariable=coord2)

所以问题是,当我运行程序时,它们像往常一样什么也不显示.但是,只要我在其中一个条目中输入一个字符,它们就会显示该字符.我不明白为什么,他们使用不同的变量?有人可以帮我吗?

So the problem is, when i run the program they show nothing, as usual. But as soon as i enter one character in one of the entries, they both show the character(s). I don't understand why, they use different variables? Can someone help me?

    coordButton = Button(tk, text="Done! (use coordinates)", font=font1)


    equationLabel = Label(tk, text="Equation: y =", font=font1)
    equationEntry = Entry(tk, textvariable=EQ, font=font1)
    equationButton = Button(tk, text="Done! (use equation)", font=font1)

    iwantanswersCheckbox = Checkbutton(tk, text="I want m, x, b, intercept and x-intercept", font=font1)
    iwantgraphCheckbox = Checkbutton(tk, text="I want a graph", font=font1)
    info1Label = Label(tk, text="***Both boxes may be checked***", font=font2)


    #pack
    coord1Label.grid(row=0, column=0, padx=15, pady=15)
    coord2Label.grid(row=1, column=0, padx=15, pady=15)

    coord1Entry.grid(row=0, column=1, padx=5, pady=5)
    coord2Entry.grid(row=1, column=1, padx=5, pady=5)

    coordButton.grid(row=2, columnspan=2, padx=5, pady=15)


    equationLabel.grid(row=3, column=0, sticky=E, padx=5, pady=5)
    equationEntry.grid(row=3, column=1, padx=5, pady=5)
    equationButton.grid(row=4, columnspan=2, padx=5, pady=15)

    iwantanswersCheckbox.grid(row=5, columnspan=2, padx=5, pady=5)
    iwantgraphCheckbox.grid(row=6, columnspan=2)
    info1Label.grid(row=7, columnspan=2)

    os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
    tk.mainloop()

tkinter_window()

def matplotlib_window():
    import matplotlib.pyplot as plt

    coordX[0] = Xcoord1Entry.get()
    coordX[1] = Xcoord2Entry.get()
    coordY[0] = Ycoord1Entry.get()
    coordY[1] = Ycoord2Entry.get()
    plt.plot(coordX, coordY)
    plt.legend(loc=4)
    plt.xlabel("x")
    plt.ylabel("y")
    plt.show()

问题应该出现的主要代码区域(根据要求):

Main area of code where the problem should be (as requested):

import tkinter
import tkinter.font as tkFont
from tkinter import *

coord1 = "0,0"
coord2 = "0,0"


def tkinter_window():
    global coord1Entry
    global coord2Entry
    global coord1
    global coord

    tk = Tk()

    tk.title("Math Graph")

    #create
    font1 = tkFont.Font(family="Source Code Pro", size=16)
    font2 = tkFont.Font(family="Source Code Pro", size=10)

    coord1Label = Label(tk, text='X coordinate 1:\n( "x,y", no parentheses )', font=font1)
    coord2Label = Label(tk, text='Y coordinate 2:\n( "x,y", no parentheses )', font=font1)

    coord1Entry = Entry(tk, textvariable=coord1)
    coord2Entry = Entry(tk, textvariable=coord2)


    #pack
    coord1Label.grid(row=0, column=0, padx=15, pady=15)
    coord2Label.grid(row=1, column=0, padx=15, pady=15)

    coord1Entry.grid(row=0, column=1, padx=5, pady=5)
    coord2Entry.grid(row=1, column=1, padx=5, pady=5)


    tk.mainloop()

tkinter_window()

推荐答案

这是因为当您需要使用 textvariable 选项的两个不同实例时,您使用了相同的字符串a href="http://effbot.org/tkinterbook/variable.htm" rel="nofollow noreferrer">特殊 tkinter 变量(StringVar 等)

It is because you are using identical strings for the textvariable option when you need to be using two different instances of one of the special tkinter variables (StringVar, etc)

顺便说一下,您几乎不需要使用textvariable.我的建议是省略它,因为您显然没有使用它.

By the way, you almost never need to use textvariable. My advice is to omit it since you're clearly not using it.

发生这种情况是因为小部件只是在嵌入式 Tcl 解释器中实现的小部件的薄包装.textvariable 选项的字符串值在嵌入式 Tcl 解释器中被视为全局变量名.由于两个字符串相同,它们在 tcl 解释器中成为相同的变量(是的,"0.0" 作为 tcl 变量是完全有效的).

This happens because the widget is just a thin wrapper around a widget implemented in an embedded Tcl interpreter. The string value of the textvariable option is treated as a global variable name in the embedded Tcl interpreter. Since both strings are the same, they become the same variable within the tcl interpreter (and yes, "0.0" is perfectly valid as a tcl variable).

这种行为实际上就是 textvariable 能够成为如此强大的工具的原因——您可以将两个或多个小部件链接在一起,这样当一个小部件的值发生变化时,它会立即反映在另一个小部件中.此外,可以对这些变量设置跟踪,以便在读取、写入或取消设置变量时获得回调.

This behavior is actually why textvariable can be such a powerful tool -- you can link two or more widgets together so that when a value changes in one, it is immediately reflected in the other. Plus, it is possible to set traces on these variables so that you can get a callback when the variable is read, written, or unset.

然而,这在 Tcl 中编码时更有用,因为在 Tcl 中 textvariable 可以是一个普通的 Tcl 变量.在 tkinter 中,它必须是一种特殊类型的对象——StringVarIntVarDoubleVarBooleanVar -- 所以你不能将它与普通变量一起使用.

However, this is much more useful when coding in Tcl, since in Tcl a textvariable can be a normal Tcl variable. In tkinter, it must be a special type of object -- an instance of StringVar, IntVar, DoubleVar, or BooleanVar -- so you can't use it with ordinary variables.

这篇关于为什么两个 tkinter 条目使用相同的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆