May-31-2017, 09:38 PM
(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()