Why are the two tkinter entries using the same number?

后端 未结 1 862
心在旅途
心在旅途 2021-01-21 19:40
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 co         


        
相关标签:
1条回答
  • 2021-01-21 20:04

    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)

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


    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).

    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.

    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.

    0 讨论(0)
提交回复
热议问题