【问题标题】:How to call startup function from class如何从类中调用启动函数
【发布时间】:2019-06-24 00:27:30
【问题描述】:

我正在学习使用类,我想从另一个 MainApplication 类中调用我的 GetWeather 类。

在调用GetWeather.coords 时,我想从一个接收所需数据的函数开始。

以下是我目前的代码:

API_ID = '///foo///'

class GetWeather:
    def __init__(self, city, country):
        url_city = 'http://api.openweathermap.org/data/2.5/weather?q={},{}&appid={}'.format(city, country, API_ID)
        weatherdata = requests.get(url_city).json()
    def coords(self):
        print(weatherdata)

class MainApplication:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)


        self.button1 = tk.Button(self.frame, text = 'Coords', width = 25, command = GetWeather.coords('town','country'))
        self.button1.pack()


        self.frame.pack()



def main():
    root = tk.Tk()
    app = MainApplication(root)
    root.mainloop()

if __name__ == "__main__":
    main()

【问题讨论】:

  • 是什么阻碍了你做你想做的事?您需要什么样的帮助?

标签: python python-3.x class tkinter


【解决方案1】:

没有什么可以阻止您这样做,但您必须确保该函数具有所需的可用信息。由于coords()GetWeather 类的方法,因此可以通过简单地保存在创建类实例时传递给它的值,然后使用它来完成。

下面是一个示例代码,展示了如何做到这一点,它会在每次点击 Button 时调用该函数来检索数据(不像 @GeeTransit 的答案,它只在最初创建 button1 时检索一次数据)。

API_ID = '///foo///'

class GetWeather:
    def __init__(self, city, country):
        self.city = city
        self.country = country

    def coords(self):
        url_city = ('http://api.openweathermap.org/data/2.5/weather?'
                    'q={},{}&appid={}'.format(self.city, self.country, API_ID)
        weatherdata = requests.get(url_city).json()  # Call function to get data.
        print(weatherdata)


class MainApplication:
    def __init__(self, master):
        self.getweather = GetWeather('town', 'country')
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text='Coords', width=25,
                                 command=self.getweather.coords)
        self.button1.pack()
        self.frame.pack()


def main():
    root = tk.Tk()
    app = MainApplication(root)
    root.mainloop()

if __name__ == "__main__":
    main()

【讨论】:

    【解决方案2】:

    要创建一个类的实例,调用它就像调用一个恰好返回 GetWeather 对象的函数一样。

    另外,tkinter.Buttoncommand 的参数应该是它可以再次调用的东西,这意味着你应该将一个尚未调用的函数传递给它。注意GetWeather('town','country').print_coords 后面缺少的括号。

    这是您的固定代码:

    import tkinter as tk
    
    import requests
    
    API_ID = '///foo///'
    
    class GetWeather:
    
        def __init__(self, city, country):
            url_city = 'http://api.openweathermap.org/data/2.5/weather?q={},{}&appid={}'.format(city, country, API_ID)
            # note: save the weather data in 'self'
            self.weatherdata = requests.get(url_city).json()
    
        def print_coords(self):
            # get the stored weather data
            print(self.weatherdata)
    
    
    class MainApplication:
    
        def __init__(self, master):
            self.master = master
            self.frame = tk.Frame(self.master)
    
            self.button1 = tk.Button(
                self.frame, text='Coords', width=25, 
                command=GetWeather('town', 'country').coords,
            )
            self.button1.pack()
    
            self.frame.pack()
    
    
    def main():
        root = tk.Tk()
        app = MainApplication(root)
        root.mainloop()
    
    if __name__ == "__main__":
        main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2020-02-25
      相关资源
      最近更新 更多