python的tkinter中的variable封装成类后调用无效?

学校要求开发一个小程序,目前我做了个查询天气的,当查询天气单独使用时,调用正常,可是import查询天气后,就出了bug

主程序代码:

from tkinter import *
import WEATHER

class main_menu:
    def __init__(self, master):
        master.title('主菜单')
        master.geometry('250x100')
        master.resizable(width=False, height=False)
        F1 = Frame(master)
        F2 = Frame(master)
        F1.pack(side=TOP)
        F2.pack(side=BOTTOM)

        weather_buttom = Button(F1, text='天气查询', command=self.init_weather)
        express_buttom = Button(F1, text='快递查询')
        plane_buttom = Button(F2, text='机票查询')
        train_buttom = Button(F2, text='车票查询')
        weather_buttom.pack(side=LEFT, pady=10, padx=10)
        express_buttom.pack(side=RIGHT, pady=10, padx=10)
        plane_buttom.pack(side=LEFT, pady=10, padx=10)
        train_buttom.pack(side=RIGHT, pady=10, padx=10)


    def init_weather(self):
        weather_window = Tk()
        weather = WEATHER.Weather(weather_window)
        weather_window.mainloop()



if __name__ == '__main__':
    tk = Tk()
    main = main_menu(tk)
    tk.mainloop()

天气查询实现的代码:

import time
from selenium import webdriver  #导入Selenium的webdriver
from selenium.webdriver.common.keys import Keys  #导入Keys
from tkinter import *

class Weather:
    def __init__(self, master):
        master.title('天气查询')
        master.resizable(width=False, height=False)
        F1 = Frame(master)
        F2 = Frame(master)
        F1.pack(side=TOP)
        F2.pack(side=BOTTOM)

        self.var = StringVar()
        self.var.set('aaaaa')

        self.e = Entry(F1)
        self.e.pack(side=LEFT, padx=5, pady=5)

        b = Button(F1, text='确认', command=self.__search)
        b.pack(side=RIGHT, padx=5, pady=5)

        L = Label(F2, textvariable=self.var, height=3)
        L.pack(side=BOTTOM)


    def __search(self):
        city = self.e.get()
        print('查询需要一定时间,请稍候...')

        driver = webdriver.PhantomJS()  #指定使用的浏览器,初始化webdriver
        driver.get("http://www.tianqi.com/")  #请求网页地址
        assert "天气" in driver.title  #看看 关键字 是否在网页title中,如果在则继续,如果不在,程序跳出。
        search = driver.find_element_by_xpath('//*[@id="index_serch"]/input')  #找到xpath元素,这里是个搜索框
        search.clear()  #清空搜索框中的内容
        search.send_keys(city)  #在搜索框中输入city
        search.send_keys(Keys.RETURN)  #相当于回车键,提交

        temperature_text = driver.find_element_by_xpath('/html/body/div[5]/div/div[1]/dl/dd[3]/p/b').text
        temperature_now = '%s当前温度:%s ℃'%(city, temperature_text)
        weather_text = driver.find_element_by_xpath('/html/body/div[5]/div/div[1]/dl/dd[3]/span/b').text
        weather_now = '%s当前天气:%s'%(city, weather_text)

        text = '查询城市:%s\n' \
               '%s\n' \
               '%s'%(city, weather_now, temperature_now)
        self.var.set(text)
        print(text)

        driver.close()  #关闭webdriver



if __name__ == '__main__':
    root = Tk()
    weather = Weather(root)
    root.mainloop()

查询天气中的self.var在把查询天气单独使用时,可以正常赋值,而当在menu中调用查询天气,self.var就无效了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!