2
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()
Cedric
  • 31
  • 1
  • 9
  • 1
    Please create a [mcve] to the problem you're having as opposed to sliced code pieces. – Nae Jan 20 '18 at 02:47
  • You may want to see [this](https://stackoverflow.com/q/47334885/7032856) for deciding when to use `textvariable` option. – Nae Jan 20 '18 at 03:49

1 Answers1

3

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.

Nae
  • 14,209
  • 7
  • 52
  • 79
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • K, thanks, i don't really understand how that connected the two entries together but i replaced the "o,o"'s by "". – Cedric Jan 20 '18 at 03:06
  • I found this to be an odd behavior, what's the fundamental reason this happens? – Nae Jan 20 '18 at 03:24
  • 1
    @Nae: I've updated my answer to try to briefly describe what is happening. – Bryan Oakley Jan 20 '18 at 03:39
  • 1
    @Cedric: if you replace `"0.0"` with `""`, you might as well not use `textvariable` at all. The _only_ reason to use `textvariable` is if you want to access it later. Otherwise it serves no purpose whatsoever – Bryan Oakley Jan 20 '18 at 03:42
  • 1
    The reason to use `textvariable` is so you can update it later. It's all part of what is effectively Tk's native [MVP](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter) system. Tkinter code doesn't usually take good advantage of it (or it would use the `trace` method of `StringVar` more). – Donal Fellows Jan 20 '18 at 09:48
  • 1
    True, but since you can update the widgets without the `textvariable`, in tkinter it's fairly useless unless you're going to add a trace or unless you explicitly want to tie two or more widgets together. – Bryan Oakley Jan 20 '18 at 14:04