Reputation: 160
I'm trying to run the function "Animals" when I click button 2 but run it with the variable "infoval" = 1. Button 3 works correctly but I'm trying to consolidate my code. I'm going to have a lot of buttons and each of them just needs to run "Animals" with a different number for "infoval". The end goal is to type in the animal name and click on the information you want and have it pop up for you. Like I said, the only issue is changing "infoval" for each separate button.
import requests
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings()
import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
def Animals():
Animal = input1.get()
Animal2=Animal.replace(' ','-')
page = requests.get('https://a-z-animals.com/animals/' +
Animal2.lower(),verify=False)
soup = BeautifulSoup(page.text, 'html.parser')
(soup.prettify())
(soup.find_all('table',class_="az-facts"))
for tr in soup.find_all('table', class_="az-facts"):
tds = tr.find_all('td')
infoval = ()
anidata = ()
x=(len(tds))
if x == 75:
if infoval == 1:
anidata = 37
elif infoval == 2:
anidata = 35
elif infoval == 3:
anidata = 33
elif infoval == 4:
anidata = 40
elif infoval == 5:
anidata = 26
elif infoval == 6:
anidata = 44
elif infoval == 7:
anidata = 29
else:
if infoval == 1:
anidata = 24
elif infoval == 2:
anidata = 22
elif infoval == 3:
anidata = 20
elif infoval == 4:
anidata = 18
elif infoval == 5:
anidata = 39
elif infoval == 6:
anidata = 45
elif infoval == 7:
anidata = 33
errorcorrect = (tds[11].text)
errorcorrect2 = 'Scientific Name:Comprised of the genus followed by the species'
if errorcorrect == errorcorrect2:
anidata = anidata - 2
birdcorrect = (tds[21].text)
birdcorrect2 = 'Wingspan:'
birdcorrect3 = 'Wing Span:The measurement from one wing tip to the other'
if birdcorrect == birdcorrect2:
if anidata>21:
anidata = anidata + 2
elif birdcorrect == birdcorrect3:
if anidata>21:
anidata = anidata + 2
label1.configure(text= (tds[anidata].text))
#----------------------------------------------------------------
def gui():
root=tk.Tk()
root.withdraw()
guiwindow = Toplevel()
guiwindow.title('Test Animal Application')
guiwindow.geometry('600x600')
label1 = tk.Label(guiwindow, text= 'hi')
label1.grid(column=1, row=3)
label2 = tk.Label(guiwindow, text= 'Please enter animal: ')
label2.grid(column=0, row=0)
input1= Entry(guiwindow, width=5)
input1.grid(column=1,row=0)
def button1click():
label1.configure(text= input1.get())
def button2click():
infoval = 1
Animals()
def button3click():
Animal = input1.get()
Animal2=Animal.replace(' ','-')
page = requests.get('https://a-z-animals.com/animals/' + Animal2.lower(),verify=False)
soup = BeautifulSoup(page.text, 'html.parser')
(soup.prettify())
(soup.find_all('table',class_="az-facts"))
for tr in soup.find_all('table', class_="az-facts"):
tds = tr.find_all('td')
infoval = 2
anidata=()
x=(len(tds))
if x == 75:
if infoval == 1:
anidata = 37
elif infoval == 2:
anidata = 35
elif infoval == 3:
anidata = 33
elif infoval == 4:
anidata = 40
elif infoval == 5:
anidata = 26
elif infoval == 6:
anidata = 44
elif infoval == 7:
anidata = 29
else:
if infoval == 1:
anidata = 24
elif infoval == 2:
anidata = 22
elif infoval == 3:
anidata = 20
elif infoval == 4:
anidata = 18
elif infoval == 5:
anidata = 39
elif infoval == 6:
anidata = 45
elif infoval == 7:
anidata = 33
errorcorrect = (tds[11].text)
errorcorrect2 = 'Scientific Name:Comprised of the genus followed by the species'
if errorcorrect == errorcorrect2:
anidata = anidata - 2
birdcorrect = (tds[21].text)
birdcorrect2 = 'Wingspan:'
birdcorrect3 = 'Wing Span:The measurement from one wing tip to the other'
if birdcorrect == birdcorrect2:
if anidata>21:
anidata = anidata + 2
elif birdcorrect == birdcorrect3:
if anidata>21:
anidata = anidata + 2
label1.configure(text= (tds[anidata].text))
button2 = tk.Button(guiwindow, text= 'Top Speed', command=button2click)
button2.grid(column=2, row=4)
button3 = tk.Button(guiwindow, text= 'Average Weight', command=button3click)
button3.grid(column=2, row=8)
button1 = tk.Button(guiwindow, text='Start Application',
command=button1click)
button1.grid(column=2, row=1)
weatherimage =
ImageTk.PhotoImage(Image.open(r'C:\Users\gregory_c\Pictures\animals.gif'))
weatherpic= tk.Label(guiwindow,image=weatherimage)
weatherpic.grid(column=6, row=2)
quitbutton = tk.Button(guiwindow, text="Exit", command=guiwindow.destroy)
quitbutton.grid(column=3,row=3)
guiwindow.mainloop()
gui()
Upvotes: 0
Views: 54
Reputation: 463
Pass infoval as a parameter to the function:
def Animals(infoval):
......
......
and then when calling the function pass in the different values for each button:
def button2click():
Animals(1)
Upvotes: 2