smaude smaude -4 years ago 80
Python Question

tkinker - Can i avoid looping through the entire program?

I am trying to make a graphing calculator in python using tkinter. I have a label made to show the equation the user is graphing, and i want to update the label every time a button is clicked. I'm very new with tkinter and one of the problems I'm facing is that when the program loops, it refreshes a list that i have to it's initial conditions. Is there a way use the tkinter mainloop to loop through only a portion of the program so that my list stops refreshing? Thank you very much! I'm trying my best!

from tkinter import *
import math
mat=["hello"]
root = Tk()

height=400
width=420

buttonframe=Frame(root)
buttonframe.pack(side=BOTTOM)

canvas = Canvas(height=height, width=width, bg="black")
canvas.pack(anchor=NW)


w=Label(buttonframe,bg="white",text="y = " + (''.join(mat)))
w.grid(row=0,column=3)


def button0():
mat.append("0")
def button1():
mat.append("1")
def button2():
mat.append("2")
def button3():
mat.append("3")
def button4():
mat.append("4")
def button5():
mat.append("5")
def button6():
mat.append("6")
def button7():
mat.append("7")
def button8():
mat.append("8")
def button9():
mat.append("9")
def buttonsin(var):
mat.append("(math.sin("+ var +"))")
def buttoncos(var):
mat.append("(mat.cos("+ var + "))")
def buttontan():
mat.append("(mat.tan("+ var + "))")

for x in range(-(int(width/2)),(int(width/2))):
x1=(width/2)+x
x2=(width/2)+x+1
y = (height/2)-(math.sin(x))
y2= (height/2)-(math.sin(x+1))
canvas.create_line(x1,y,x2,y2,fill="green",dash=(5,5))

#buttons
button1 = Button(buttonframe, text="0", width=10,height=2,bg="light blue",command=button0)
button1.grid(row=1,column=3)

button2 = Button(buttonframe, text="1", width=10,height=2,bg="light blue",command=button1)
button2.grid(row=1,column=4)

button3 = Button(buttonframe,text="2", width=10,height=2,bg="light blue",command=button2)
button3.grid(row=1,column=5)

button4 = Button(buttonframe,text="sine", width=10,height=2,bg="light blue",command=buttonsin)
button4.grid(row=1,column=6)

button5 = Button(buttonframe,text="cosine", width=10,height=2,bg="light blue",command=buttoncos)
button5.grid(row=1,column=7)

button6 = Button(buttonframe,text="tangent", width=10,height=2,bg="light blue",command=buttontan)
button6.grid(row=1,column=8)

#next row
button7 = Button(buttonframe,text="3", width=10,height=2,bg="light blue",command=button3)
button7.grid(row=2,column=3)

button1 = Button(buttonframe, text="4", width=10,height=2,bg="light blue",command=button4)
button1.grid(row=2,column=4)

button2 = Button(buttonframe, text="5", width=10,height=2,bg="light blue",command=button5)
button2.grid(row=2,column=5)

button3 = Button(buttonframe,text="x^2", width=10,height=2,bg="light blue")
button3.grid(row=2,column=6)

button4 = Button(buttonframe,text="x^y", width=10,height=2,bg="light blue")
button4.grid(row=2,column=7)

button5 = Button(buttonframe,text="sqrt", width=10,height=2,bg="light blue")
button5.grid(row=2,column=8)

#next row
button6 = Button(buttonframe,text="6", width=10,height=2,bg="light blue",command=button6)
button6.grid(row=3,column=3)

button7 = Button(buttonframe,text="7", width=10,height=2,bg="light blue",command=button7)
button7.grid(row=3,column=4)

button5 = Button(buttonframe,text="8", width=10,height=2,bg="light blue",command=button8)
button5.grid(row=3,column=5)

button6 = Button(buttonframe,text="button6", width=10,height=2,bg="light blue")
button6.grid(row=3,column=6)

button7 = Button(buttonframe,text="button7", width=10,height=2,bg="light blue")
button7.grid(row=3,column=7)

button1 = Button(buttonframe, text="button", width=10,height=2,bg="light blue")
button1.grid(row=3,column=8)

#next row
button2 = Button(buttonframe, text="9", width=10,height=2,bg="light blue",command=button9)
button2.grid(row=4,column=3)

button3 = Button(buttonframe,text="+", width=10,height=2,bg="light blue")
button3.grid(row=4,column=4)

button4 = Button(buttonframe,text="-", width=10,height=2,bg="light blue")
button4.grid(row=4,column=5)

button5 = Button(buttonframe,text="button5", width=10,height=2,bg="light blue")
button5.grid(row=4,column=6)

button6 = Button(buttonframe,text="button6", width=10,height=2,bg="light blue")
button6.grid(row=4,column=7)

button6 = Button(buttonframe,text="button6", width=10,height=2,bg="light blue")
button6.grid(row=4,column=8)

root.mainloop()

Answer Source

mainloop does not loop through your code. Your program is executed once. mainloop is simply a loop that processes events in a queue, it doesn't continuously try to run your code.

The problem is not that the list isn't being updated. The list is being updated. You're not doing anything to cause that list to be displayed. When you configure a label to display the list, you are ultimately giving it a static string. You need to explicitly reset the string on the label.

def button0():
    mat.append("0")
    w.configure(text="y = " + (''.join(mat)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download