E
E
ElemAnybody2019-11-22 14:56:37
Python
ElemAnybody, 2019-11-22 14:56:37

Python how to fix my error (tkinter variables in matlab)?

How can I make it so that when I enter values ​​in "txt" and "txt2" they are stored in variables so that I can use them in "def next():" [in my example this is to put a dot with the code " plt.plot (x,y, '--bo') "]

import matplotlib.pyplot as plt
from tkinter import*
import tkinter

def start():

    window = Tk()

    window.title("Ввод точек!")

    window.geometry('400x500')

    lbl = Label(window, text="1")
    lbl.grid(column=0, row=0)
    txt = Entry(window, width=10)
    lbl2 = Label(window, text="2")
    lbl2.grid(column=0, row=1)
    txt2 = Entry(window, width=10)
    txt2.grid(column=4, row=0)
    txt.grid(column=4, row=1)
    btn = Button(window, text="3", command = next)
    btn.config(height=20, width=45)
    btn.grid(column=0, row=5)
    lbl3 = Label(window, text="4")
    lbl3.grid(column=0, row=6)
    txt3 = Entry(window, width=45)
    txt3.grid(column=0, row=7)
    window.mainloop()
    global x
    global y
    x = txt.get()
    y = txt2.get()
def next():
    plt.subplot(111)
    plt.plot([0, 40, 40], [20, 20, 0],'--bo')
    plt.plot([0, 37, 37], [12, 12, 0])
    plt.axis([0, 100, 0, 60])
    plt.plot(x,y, '--bo')
    plt.title('Seed et al. (2003)')
    plt.show()
start()

It throws the following error:
plt.plot(x,y, '--bo')

NameError : name 'x' is not defined : NameError: name 'txt' is not defined )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
ElemAnybody, 2019-11-22
@ElemAnybody

import matplotlib.pyplot as plt
from tkinter import*
import tkinter
x = 0
y = 0

def next():
    global x
    global y


    plt.subplot(111)
    # 'ro'
    amin = 12
    bmax = 20
    cmin = 37
    dmax = 47
    p1 = 2
    p2 = 2
    plt.plot([0, 40, 40], [20, 20, 0],'--bo')
    plt.plot([0, 37, 37], [12, 12, 0])
    plt.axis([0, 100, 0, 60])
    plt.plot([int(txt.get())], [int(txt2.get())],'--bo')
    plt.title('Seed et al. (2003)')
    plt.show()

window = Tk()

window.title("Ввод точек!")

window.geometry('400x500')

lbl = Label(window, text="1")
lbl.grid(column=0, row=0)
txt = Entry(window, width=10)

lbl2 = Label(window, text="2")
lbl2.grid(column=0, row=1)
txt2 = Entry(window, width=10)

txt2.grid(column=4, row=0)
txt.grid(column=4, row=1)

btn = Button(window, text="3", command=next)
btn.config(height=20, width=45)
btn.grid(column=0, row=6)
lbl3 = Label(window, text="4")
lbl3.grid(column=0, row=7)
txt3 = Entry(window, width=45)
txt3.grid(column=0, row=8)
window.mainloop()

R
res2001, 2019-11-22
@res2001

Define x and y in the global scope.
It looks like global doesn't create variables, it just tells python to look for those variables in the global scope. If the variable is not found there, it is created according to the usual rules - in the local scope.
https://docs.python.org/3/reference/simple_stmts.h...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question