Tkinter function print in GUI

前端 未结 2 1772
野性不改
野性不改 2021-01-22 16:49

I have done a program with 2 apis that show forecast and city info of diffrent citys that the user choose. But now I need help cause Im stuck on how to get my make_request, make

相关标签:
2条回答
  • 2021-01-22 17:04

    If you just want to simulate printing to the console, you should use a text widget. The text widget is the right choice for showing multi-line data. Instead of calling print, you call insert:

    def create_widgets(self):
        ...
        self.out = Text(self)
        ...
    
    def make_request(self):
        r = requests.get(...)
        data = r.json()
        self.out.insert("end", data)
        ...
    

    If you want to show just a few pieces of data, you can use label widgets, and change what appears in them using the config method. For example:

    def create_widgets(self):
        ...
        self.label1 = Label(self, ...)
        self.label2 = Label(self, ...)
    
    def make_request(self):
        ...
        self.label1.configure(text=something)
        self.label2.configure(text=something_else)
    
    0 讨论(0)
  • 2021-01-22 17:22

    Example - I use existing label with title to set information from request

    def make_request(self):
        r = requests.get("http://api.wunderground.com/api/61418d709872f773/forecast/q/" + self.enterCountry.get() +"/" + self.v.get() +".json")
        data = r.json()
        for day in data['forecast']['simpleforecast']['forecastday']:
            self.var.set( day['date']['weekday'] + ":" + "\n" + "Conditions: " + day['conditions'] + "\n" + "High: " + day['high']['celsius'] + "C " + "Low: " + day['low']['celsius'] + "C" )
        return data
    
    0 讨论(0)
提交回复
热议问题