Skip to content Skip to sidebar Skip to footer

Why Are The Two Tkinter Entries Using The Same Number?

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 glo

Solution 1:

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.

Post a Comment for "Why Are The Two Tkinter Entries Using The Same Number?"