Python Forum
[Tkinter] Clearing Text in Tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Clearing Text in Tkinter (/thread-3525.html)



Clearing Text in Tkinter - icabero0225 - May-31-2017

I'm writing a program to assist me in stock trading, and I wanted to make it have a nice GUI so my buddies can use it as well. I just started learning Tkinter yesterday, so my code is not perfect. Here's what I got:
from tkinter import *

#Functions
def buy():

   datr = float(datrEntry.get())
   dzd = float(dzdEntry.get())
   dzp = float(dzpEntry.get())
   szp = float(szpEntry.get())
   szd = float(szdEntry.get())
   wr = (datr * 0.02)
   sl = (dzd - wr)
   rsk = (dzp - sl)
   rwd = (rsk * 3)
   t1 = (dzp + rwd)
   t2 = szp

   global buyText, buyEntryText, buySlText, buyT1Text
   buyText=Label(root, text="Buy\n", fg="green")
   buyEntryText= Label(root, text="Entry: "+str(dzp))
   buySlText= Label(root, text="Stop Loss: "+str(sl))
   buyT1Text=Label(root, text="Target: "+str(t1)+"\n")
   buyText.grid(sticky=W)
   buyEntryText.grid(sticky=W)
   buySlText.grid(sticky=W)
   buyT1Text.grid(sticky=W)
def sell():
   exit() #I'm not done coding this part yet

def clearScreen():
   buyText.grid_forget()
   buyEntryText.grid_forget()
   buySlText.grid_forget()
   buyT1Text.grid_forget()

#Window
root = Tk()

root.title("Trading Engine")

menu = Menu(root)
root.config(menu=menu)

fileMenu = Menu(menu)
menu.add_cascade(label="File", menu = fileMenu)
fileMenu.add_command(label="Exit", command=exit)

datrLabel=Label(root, text="Daily Average True Range")
datrEntry = Entry(root)
dzdLabel = Label(root, text="Demand Zone Distal")
dzdEntry = Entry(root)
dzpLabel=Label(root, text="Demand Zone Proximal")
dzpEntry=Entry(root)
szpLabel=Label(root, text="Supply Zone Proximal")
szpEntry=Entry(root)
szdLabel=Label(root, text="Supply Zone Distal")
szdEntry=Entry(root)

datrLabel.grid(row=0, column=0,sticky=E)
datrEntry.grid(row=0, column=1)
dzdLabel.grid(row=2, column=0,sticky=E)
dzdEntry.grid(row=2, column=1)
dzpLabel.grid(row=4, column=0,sticky=E)
dzpEntry.grid(row=4, column=1)
szpLabel.grid(row=6, column=0,sticky=E)
szpEntry.grid(row=6, column=1)
szdLabel.grid(row=8, column=0,sticky=E)
szdEntry.grid(row=8, column=1)

toolbar = Frame(root)
toolbar.grid(sticky=W, padx=15, pady=10, columnspan=3)
buyButton = Button(toolbar, text="Buy", command=buy, relief=RAISED, borderwidth=2, bg="green")
sellButton = Button(toolbar, text="Sell", command=sell, relief=RAISED, borderwidth=2, bg="red")
clearButton = Button(toolbar, text="Clear", command=clearScreen)
buyButton.grid()
sellButton.grid()
clearButton.grid()

root.geometry("500x500")
root.mainloop()
I know, I know, it's a mess. Basically, I'm trying to get the labels buyText, buyEntryText, buySlText, and buyT1Text that print after the buy() function completes to clear everytime I click the buy or sell button again. I don't really know how to explain this any better, so you can ask any questions to clarify if you want.


RE: Clearing Text in Tkinter - metulburr - May-31-2017

You need to use textvariable

You need to change your structure in how you are doing things. For example you are creating a new label every time you callback, instead just make the one label, and set it in the callback. Also you should really consider using classes (OOP).

Here is a small example using textvariable. Keep pressing the button and the label changes based on the current epoch timestamp.
import tkinter as tk
import time

def callback():
    lbl.set('{}'.format(time.time()))
    
root = tk.Tk()
lbl = tk.StringVar()
lbl.set('default')
tk.Label(root, textvariable=lbl).pack()
tk.Button(root, text="Get Time", command=callback).pack()


root.mainloop()
    



RE: Clearing Text in Tkinter - icabero0225 - May-31-2017

(May-31-2017, 03:03 AM)metulburr Wrote: You need to use textvariable

You need to change your structure in how you are doing things. For example you are creating a new label every time you callback, instead just make the one label, and set it in the callback. Also you should really consider using classes (OOP).

Here is a small example using textvariable. Keep pressing the button and the label changes based on the current epoch timestamp.
import tkinter as tk
import time

def callback():
    lbl.set('{}'.format(time.time()))
    
root = tk.Tk()
lbl = tk.StringVar()
lbl.set('default')
tk.Label(root, textvariable=lbl).pack()
tk.Button(root, text="Get Time", command=callback).pack()


root.mainloop()
    

Thank you so much, after reading some documentation I decided to rewrite the program. Here it is:
from tkinter import *

global datr,dzd,dzp,szp,szd,wr,sl,rsk,rwd,t1,t2

def buy():
    datr = float(datrEntry.get())
    dzd = float(dzdEntry.get())
    dzp = float(dzpEntry.get())
    szp = float(szpEntry.get())
    szd = float(szdEntry.get())
    wr = (datr * 0.02)
    sl = (dzd - wr)
    rsk = (dzp - sl)
    rwd = (rsk * 3)
    t1 = (dzp + rwd)
    t2 = szp

    orderLbl.set("Buy")
    dzpLbl.set(dzd)
    slLbl.set(sl)
    t1Lbl.set(t1)

def sell():
    datr = float(datrEntry.get())
    dzd = float(dzdEntry.get())
    dzp = float(dzpEntry.get())
    szp = float(szpEntry.get())
    szd = float(szdEntry.get())
    wr = (datr * 0.02)
    sl = (szd + wr)
    rsk = (sl - szp)
    rwd = (rsk * 3)
    t1 = (szp - rwd)
    t2 = dzp

    orderLbl.set("Sell")
    szpLbl.set(szp)
    slLbl.set(sl)
    t1Lbl.set(t1)

root=Tk()
root.title("Trading Engine")
root.resizable(width=False, height=False)

#DATR
Label(root, text="Daily Average True Range").grid(row=2, column=0,sticky=E)
datrEntry = Entry(root)
datrEntry.grid(row=2,column=1)
datrEntry.focus_set()
#DZD
Label(root, text="Demand Zone Distal").grid(row=4, column=0,sticky=E)
dzdEntry = Entry(root)
dzdEntry.grid(row=4,column=1)
#DZP
Label(root, text="Demand Zone Proximal").grid(row=6, column=0,sticky=E)
dzpEntry = Entry(root)
dzpEntry.grid(row=6,column=1)
#SZP
Label(root, text="Supply Zone Proximal").grid(row=8, column=0,sticky=E)
szpEntry = Entry(root)
szpEntry.grid(row=8,column=1)
#SZD
Label(root, text="Supply Zone Distal").grid(row=10, column=0,sticky=E)
szdEntry = Entry(root)
szdEntry.grid(row=10,column=1)
#Entries
datrLbl = StringVar()
dzdLbl = StringVar()
dzpLbl = StringVar()
szpLbl = StringVar()
szdLbl = StringVar()
slLbl = StringVar()
t1Lbl = StringVar()
orderLbl = StringVar()

Button(root, text="Buy", command=buy,bg="green").grid(sticky=W)
Button(root, text="Sell", command=sell,bg="red").grid(sticky=W)

Label(root).grid(row=12,column=0)#blank space
orderLbl.set("")
dzpLbl.set("")
slLbl.set("")
t1Lbl.set("")


Label(root, text="Order Type: ").grid(row=14,column=0,sticky=W)
Label(root, textvariable=orderLbl).grid(row=14,column=1,sticky=W)
Label(root).grid(row=16, column=0) # Blank space
Label(root, text="Entry: ").grid(row=18, column=0, sticky=W)
Label(root, textvariable=dzpLbl).grid(row=18, column=1, sticky=W)
Label(root, text="Stop Loss: ").grid(row=20, column=0, sticky=W)
Label(root, textvariable=slLbl).grid(row=20, column=1, sticky=W)
Label(root, text="Target: ").grid(row=22, column=0, sticky=W)
Label(root, textvariable=t1Lbl).grid(row=22, column=1, sticky=W)

root.geometry("500x500")
#root.geometry("330x250")
root.mainloop()