首页 > 解决方案 > Tkinter 问题。按钮操作前的输出显示

问题描述

我正在使用 Tkinter 进行实验。我写了一个小代码,当我按下一个特定的按钮时应该做一些工作。我制作了五个按钮和五个后续功能。在viewlivescores的第一个按钮中,我遇到了一个问题,因为我什至在界面开始之前就看到了该部分的输出。我如何更正此部分,以便仅在单击按钮后才能运行 viewlivescores 功能。

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()

我的 GUI 和输出的屏幕截图是 在此处输入图像描述

在此处输入图像描述

标签: user-interfacetkinterweb-scrapingpython-3.8

解决方案


推荐阅读