在更改变量的同时重新运行功能

时间:2018-08-01 17:32:44

标签: python python-3.x

当我单击按钮2时,我试图运行功能“动物”,但是使用变量“ infoval” = 1来运行它。按钮3可以正常工作,但是我试图合并我的代码。我将有很多按钮,每个按钮只需要运行带有“ infoval”的不同编号的“ Animals”即可。 最终目标是输入动物名称,然后单击所需的信息,然后弹出该信息。就像我说的那样,唯一的问题是更改每个单独按钮的“ infoval”。

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

1 个答案:

答案 0 :(得分:2)

将infoval作为参数传递给函数:

def Animals(infoval):
......
......

,然后在调用函数时为每个按钮传递不同的值:

def button2click():
    Animals(1)