0

I am using experimenting with Tkinter. I have Written a small code that should do some work when I press a particular button. I have made Five Buttons and Five subsequent functions. In the First button for viewlivescores I am getting an issue as I am seeing output of the section even before the start of interface. How I can correct this section so that only after button click the function viewlivescores should run.

from tkinter import *
from PIL import ImageTk,Image #PIL -> Pillow
from tkinter import messagebox

import pandas as pd


import requests
from html.parser import HTMLParser
from bs4 import BeautifulSoup

from urllib.request import urlopen

import networkx as nx
from tqdm import tqdm
import warnings
from statistics import median
import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen


site = 'http://static.cricinfo.com/rss/livescores.xml'

def viewlivescores():
    messagebox.showinfo("This is The Live Scores Interface")
    print("You are in Live scores Interface")


    
#Open that site

site = 'http://static.cricinfo.com/rss/livescores.xml'    
op = urlopen(site) 

# read data from site
rd = op.read() 
# # close the object
op.close()   
# scrapping data from site
sp_page = soup(rd,'xml') 

match_list = sp_page.find_all('description') 
# print(match_list)
for match in match_list:  
    print(match.get_text())     



def viewteamrecords():
    messagebox.showinfo("This is team Records Interface")
    print("You are in Live scores Interface")


   
     
    
def viewplayerrecords():
    messagebox.showinfo("This is Player Records Interface")
    print("You are in Player Records Interface")
    


def viewpartnershipgraph():
    messagebox.showinfo("This is Overall Records Interface")
    print("You are in Partnerships Record Interface")
 

  

def exitmenu():
    print("Thank You for Choosing the Program")
    root.destroy()




#Main Section
root = Tk()
root.title("Criclytics")
root.minsize(width=400,height=400)
root.geometry("600x500")

# Take n greater than 0.25 and less than 5
same=True
n=2.8

# Adding a background image
background_image =Image.open("Cricket.jpg")
[imageSizeWidth, imageSizeHeight] = background_image.size

newImageSizeWidth = int(imageSizeWidth*n)
if same:
    newImageSizeHeight = int(imageSizeHeight*n) 
else:
    newImageSizeHeight = int(imageSizeHeight/n) 
    
background_image = background_image.resize((newImageSizeWidth,newImageSizeHeight),Image.ANTIALIAS)
img = ImageTk.PhotoImage(background_image)
Canvas1 = Canvas(root)
Canvas1.create_image(300,340,image = img)      
Canvas1.config(bg="white",width = newImageSizeWidth, height = newImageSizeHeight)
Canvas1.pack(expand=True,fill=BOTH)

headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.2,rely=0.1,relwidth=0.6,relheight=0.16)
headingLabel = Label(headingFrame1, text="Welcome to \n Cricklytics", bg='black', fg='white', font=('Courier',15))
headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)



btn1 = Button(root,text="View Live Scores",bg='black', fg='white',command=viewlivescores)
btn1.place(relx=0.28,rely=0.4, relwidth=0.45,relheight=0.1)
    

btn2 = Button(root,text="View Team Records",bg='black', fg='white',command=viewteamrecords)
btn2.place(relx=0.28,rely=0.5, relwidth=0.45,relheight=0.1)


btn3 = Button(root,text="View Player Records",bg='black', fg='white',command=viewplayerrecords)
btn3.place(relx=0.28,rely=0.6, relwidth=0.45,relheight=0.1)



btn4 = Button(root,text="View Partnership Record",bg='black', fg='white',command=viewpartnershipgraph)
btn4.place(relx=0.28,rely=0.7, relwidth=0.45,relheight=0.1)



btn5 = Button(root,text="Exit The Menu",bg='black', fg='white',command=exitmenu)
btn5.place(relx=0.28,rely=0.8, relwidth=0.45,relheight=0.1)


root.mainloop()

The Screenshots of my GUI and Output are enter image description here

enter image description here

  • Does this answer your question? [Why is the command bound to a Button or event executed when declared?](https://stackoverflow.com/questions/5767228/why-is-the-command-bound-to-a-button-or-event-executed-when-declared) – Matiiss Jul 28 '21 at 09:31
  • 3
    it seems like an indentation problem, are you sure some part of the code shouldn't be under the `viewlivescores` definition? also why do you repeat `site` variable, you have already defined it once, why do it again? – Matiiss Jul 28 '21 at 09:35

0 Answers0