OZmium Sports Betting and Horse Racing Forums

OZmium Sports Betting and Horse Racing Forums (http://forums.ozmium.com.au/index.php)
-   General Topics (http://forums.ozmium.com.au/forumdisplay.php?f=59)
-   -   Python help using tkinter (http://forums.ozmium.com.au/showthread.php?t=32133)

Shaun 8th October 2020 10:43 AM

Python help using tkinter
 
This is my first attempt at a python app and can't find the answers i need so thought i would post here for some help.

This app will be a replacement for my Unitab prices excel sheet, so far i can import the venues and a bit of info for the day, i have tried a few different methods and the closes i can get is it prints the first venue info and stops, yet if you run this in your ide it prints all the info as i would like it.

The print statement doesn't need to be there.

Code:
from tkinter import * from tkcalendar import Calendar,DateEntry import json import requests app = Tk() app.title("Todays Races") app.geometry("1000x500") app.resizable(width=False, height=False) date_label = Label(app, text="Input Date", font=("bold", 14), pady=20) date_label.pack() cal = DateEntry(app,width=30,bg="darkblue",fg="white",date_pattern='yyyy-mm-dd',year=2020) cal.pack() date_btn = Button(app, text="Go", width=8, command=lambda: get_json()) date_btn.pack() def get_json(): state = "VIC" date = cal.get() url = "https://api.beta.tab.com.au/v1/tab-info-service/racing/dates/" + str(date) + "/meetings?jurisdiction=" + state data = requests.get(url) data1 = json.loads(data.content) Venues = data1["meetings"] for Venue in Venues: Rtype = Venue["raceType"] track = Venue["meetingName"] Rstate = Venue["location"] Wcondition = Venue["weatherCondition"] Tcondition = Venue["trackCondition"] states = ("VIC","NSW","QLD","SA","WA","TAS","ACT") if Rtype == "R" and Rstate in states: text = track + " " + Rstate + " " + Tcondition + " " + Wcondition print(text) app.mainloop()

Shaun 8th October 2020 11:51 AM

Success......always the way, try to solve a problem for a couple days as soon as i post the issue i figure it out.

Code:
from tkinter import * from tkcalendar import Calendar,DateEntry import json import requests app = Tk() app.title("Todays Races") app.geometry("1000x500") app.resizable(width=False, height=False) date_label = Label(app, text="Input Date", font=("bold", 14), pady=20) date_label.pack() cal = DateEntry(app,width=30,bg="darkblue",fg="white",date_pattern='yyyy-mm-dd',year=2020) cal.pack() date_btn = Button(app, text="Go", width=8, command=lambda: get_json()) date_btn.pack() def get_json(): try: state = "VIC" date = cal.get() url = "https://api.beta.tab.com.au/v1/tab-info-service/racing/dates/" + str(date) + "/meetings?jurisdiction=" + state data = requests.get(url) data1 = json.loads(data.content) Venues = data1["meetings"] for Venue in Venues: Rtype = Venue["raceType"] track = Venue["meetingName"] Rstate = Venue["location"] Wcondition = Venue["weatherCondition"] Tcondition = Venue["trackCondition"] states = ("VIC","NSW","QLD","SA","WA","TAS","ACT") if Rtype == "R" and Rstate in states: text = track + " " + Rstate + " " + Tcondition + " " + Wcondition text_label = Label(app, text=str(text)) text_label.pack() except: pass app.mainloop()

Shaun 14th October 2020 12:48 AM

I have progressed a bit further but having issues with the formatting, if you run this you will notice the results are going in a downward pattern rather than lining up under the headings.

Code:
from tkinter import * from tkcalendar import Calendar,DateEntry import json import requests app = Tk() app.title("Todays Races") app.geometry("1000x500") app.configure(bg='Lightblue') app.resizable(width=False, height=False) #Clears Daily Race Meetings def Fclear(): for widget in Rinfo.winfo_children(): widget.destroy() #Collects Daily Race Meetings def get_json(): state = "VIC" date = cal.get() url = "https://api.beta.tab.com.au/v1/tab-info-service/racing/dates/" + str(date) + "/meetings?jurisdiction=" + state data = requests.get(url) data1 = json.loads(data.content) Venues = data1["meetings"] for Venue in Venues: Rtype = Venue["raceType"] Mname = Venue["meetingName"] Location = Venue["location"] Wcondition = Venue["weatherCondition"] Tcondition = Venue["trackCondition"] states = ("VIC","NSW","QLD","SA","WA","TAS","ACT") if Rtype == "R" and Location in states: #Race Info Mname_label = Label(Rinfo, text=Mname, font=("bold", 12), width=15, anchor=W, bg="Lightblue") Mname_label.grid(column=0, sticky=W) Location_label = Label(Rinfo, text=Location, font=("bold", 12), width=5 ,anchor=W, bg="Lightblue") Location_label.grid(column=2, sticky=W) Tcondition_label = Label(Rinfo, text=Tcondition, font=("bold", 12), width=10 ,anchor=W, bg="Lightblue") Tcondition_label.grid(column=3, sticky=W) Wcondition_label = Label(Rinfo, text=Wcondition, font=("bold", 12), width=10 ,anchor=W, bg="Lightblue") Wcondition_label.grid(column=4, sticky=W) # Date Selection Frame Dateframe = Frame(app, bg="Lightblue") Dateframe.grid(row=0, column=0, sticky=W) #Frame widgets date_label = Label(Dateframe, text="Input Date", font=("bold", 14), bg="Lightblue") date_label.grid(row=0, column=0) cal = DateEntry(Dateframe,width=15, bg="darkblue", fg="white", date_pattern='yyyy-mm-dd',year=2020) cal.grid(row=0, column=2) date_btn = Button(Dateframe, text="Go", width=8, bg="Lightgreen", command=get_json) date_btn.grid(row=0, column=3) delete_btn = Button(Dateframe, text="Clear", width=8, bg="#ff704d", command=Fclear) delete_btn.grid(row=0, column=4) #Headings frame Headingsframe = Frame(app, bg="Lightblue") Headingsframe.grid(row=1, column=0, sticky=W) #Race Info Headings htrack = Label(Headingsframe, text="Track", pady=10, font=("bold", 14),anchor=W, width=15, fg="Blue", bg="Lightblue") htrack.grid(row=0, column=0) hstate = Label(Headingsframe, text="State", pady=10, font=("bold", 14),anchor=W, width=5, fg="Blue", bg="Lightblue") hstate.grid(row=0, column=2) hcondition = Label(Headingsframe, text="Condition", pady=10, font=("bold", 14),anchor=W, width=10, fg="Blue", bg="Lightblue") hcondition.grid(row=0, column=3) hweather = Label(Headingsframe, text="Weather", pady=10, font=("bold", 14),anchor=W, width=10, fg="Blue", bg="Lightblue") hweather.grid(row=0, column=4) #Race Info Frame Rinfo = Frame(app, bg="lightblue") Rinfo.grid(row=2, column=0, sticky=W) app.mainloop()

chook 14th October 2020 03:38 AM

try .strip()
on the entries

chook 14th October 2020 03:58 AM

my early morning guess does not work
to make debug easier, change background colours
just do a think like
MNA_COLOR = "Red"
LOC_COLOR = "Blue"
and use that in places like your race info
then, when you are all filled with joy, change them to what you really want

it is a long time since i used tkinter
i will see if i can find a better answer for you

chook 14th October 2020 05:30 AM

label.grid( row=0,column=0,sticky=w)

Shaun 14th October 2020 09:08 AM

I will try your background solution, it might let me see what's going on, as for adding the row this seems to stop the loop as it always prints on row 0

Shaun 14th October 2020 10:19 AM

I solved this by creating a frame and spanning the column, i then just packed everything in from the left then just looped over and did it again.

Setting the colours definitely made a difference as i could see what was happening, now just to set them all back.

Code:
from tkinter import * from tkcalendar import Calendar,DateEntry import json import requests app = Tk() app.title("Todays Races") app.geometry("1000x500") app.configure(bg='Lightblue') app.resizable(width=False, height=False) #Clears Daily Race Meetings def Fclear(): for widget in Rinfo.winfo_children(): widget.destroy() #Collects Daily Race Meetings def get_json(): state = "VIC" date = cal.get() url = "https://api.beta.tab.com.au/v1/tab-info-service/racing/dates/" + str(date) + "/meetings?jurisdiction=" + state data = requests.get(url) data1 = json.loads(data.content) Venues = data1["meetings"] for V in Venues: Rtype = V["raceType"] Mname = V["meetingName"] Location = V["location"] Wcondition = V["weatherCondition"] Tcondition = V["trackCondition"] states = ("VIC","NSW","QLD","SA","WA","TAS","ACT") if Rtype == "R" and Location in states: #Race Info frame Rinfo = Frame(app, width=100, bg="green") Rinfo.grid(columnspan=1) #Race info Mname_label = Label(Rinfo, text=Mname, font=("bold", 12), width=20, anchor=W, bg="red") Mname_label.pack(side=LEFT) Location_label = Label(Rinfo, text=Location, font=("bold", 12), width=5 ,anchor=W, bg="red") Location_label.pack(side=LEFT) Tcondition_label = Label(Rinfo, text=Tcondition, font=("bold", 12), width=10 ,anchor=W, bg="red") Tcondition_label.pack(side=LEFT) Wcondition_label = Label(Rinfo, text=Wcondition, font=("bold", 12), width=10 ,anchor=W, bg="red") Wcondition_label.pack(side=LEFT) # Date Selection Frame Dateframe = Frame(app, bg="Lightblue") Dateframe.grid(row=0, column=0, sticky=W) #Frame widgets date_label = Label(Dateframe, text="Input Date", font=("bold", 14), bg="Lightblue") date_label.grid(row=0, column=0) cal = DateEntry(Dateframe,width=15, bg="darkblue", fg="white", date_pattern='yyyy-mm-dd',year=2020) cal.grid(row=0, column=2) date_btn = Button(Dateframe, text="Go", width=8, bg="Lightgreen", command=get_json) date_btn.grid(row=0, column=3) delete_btn = Button(Dateframe, text="Clear", width=8, bg="#ff704d", command=Fclear) delete_btn.grid(row=0, column=4) #Headings frame Headingsframe = Frame(app, bg="Lightblue") Headingsframe.grid(row=1, column=0, sticky=W) #Race Info Headings htrack = Label(Headingsframe, text="Track", pady=10, font=("bold", 12),anchor=W, width=20, fg="Blue", bg="yellow") htrack.grid(row=0, column=0) hstate = Label(Headingsframe, text="State", pady=10, font=("bold", 12),anchor=W, width=5, fg="Blue", bg="orange") hstate.grid(row=0, column=2) hcondition = Label(Headingsframe, text="Condition", pady=10, font=("bold", 12),anchor=W, width=10, fg="Blue", bg="blue") hcondition.grid(row=0, column=3) hweather = Label(Headingsframe, text="Weather", pady=10, font=("bold", 12),anchor=W, width=10, fg="Blue", bg="green") hweather.grid(row=0, column=4) app.mainloop()

chook 14th October 2020 10:54 AM

#sorry for my rushed reply.
#above the for Venue in venues:
rowo = 0
for Venue in Venues:
#add row=rowo to your .grid( lines
#then at the bottom of that loop under
Wcondition_label.grid( column=4,sticky=W,row=rowo)
rowo += 1
#that will increment the row number
#you need row=rowo or whatever you choose to call it alongside the column=n

Shaun 14th October 2020 11:06 AM

I will also try this as it may be useful moving forward with this project.


All times are GMT +10. The time now is 01:17 PM.

Powered by: vBulletin Version 3.0.3
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.