首页 新闻 赞助 找找看

如何获取winfrom的文本框,并赋值

0
悬赏园豆:20 [已解决问题] 解决于 2015-12-21 10:34

我现在使用tkinter创建了一个winfrom,现在需要在其他py文件中像winfrom的文本框添加信息,请问如何实现?如果是在同一个py文件中没问题,主要是在不同py文件中。

main_application.py

from tkinter import *
import time,grab_data

class Application():
    SHOW_MSG=None
    def __init__(self):
        global SHOW_MSG
        self.app=Tk()
        SHOW_MSG=self.app
        self.app.deiconify()
    def create_app(self,title):
        self.app.title(title)
        screen_width=int(self.app.winfo_screenwidth()/2)
        screen_height=int((self.app.winfo_screenheight()-100)/2)
        self.app.tk_menuBar(self.create_menus())
        self.app.geometry('%sx%s+%s+%s' %(600,600,int((screen_width - self.app.winfo_width())/2),int((screen_height - self.app.winfo_height())/2)))#设置宽度、高度,设置窗体居中s
        self.app.resizable(width=False,height=False)#设置宽/高不可变
        self.app.mainloop()
    def create_menus(self):
        main_menu=Menu(self.app)

        sameway_menu = Menu(main_menu)#创建顶级菜单选项
        sameway_child_menu=Menu(sameway_menu,tearoff = 0)

        product_menu=Menu(sameway_child_menu,tearoff = 0)
        product_menu.add_command(label='obtain ticket')
        product_menu.add_command(label='obtain spots')

        sameway_child_menu.add_cascade(label='obtain product',menu =product_menu)
        sameway_child_menu.add_command(label='obtain price',command=grab_data.grab)
        sameway_child_menu.add_command(label='obtain price1',command=messageshow)


        main_menu.add_cascade(label='sameway',menu=sameway_child_menu)

        find_menu = Menu(main_menu)#创建顶级菜单选项
        find_child_menu=Menu(find_menu,tearoff = 0)
        find_child_menu.add_command(label='obtain price')
        find_child_menu.add_command(label='obtain price')

        main_menu.add_cascade(label='findticket',menu=find_child_menu)

        other_menu = Menu(main_menu)#创建顶级菜单选项
        other_child_menu=Menu(other_menu,tearoff = 0)
        other_child_menu.add_command(label='clear msg')
        other_child_menu.add_command(label='product shelf')

        main_menu.add_cascade(label='other',menu=other_child_menu)

        self.app['menu'] = main_menu

        self.app.text=Text(self.app,bg='black',foreground='green',font='24',name='showmsg')
        self.app.text.pack(fill=Y,expand=YES)


def messageshow(str='zcy'):
        global SHOW_MSG
        str='%s            %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),str)
        print(str)
        SHOW_MSG.text.insert(END,str)
        SHOW_MSG.text.update()
if __name__=="__main__":
    application=Application()
    application.create_app('crawler')
View Code
grab_data.py

import  main_application
def grab():
    main_application.messageshow('start grab data....')
View Code
瀞默的主页 瀞默 | 初学一级 | 园豆:149
提问于:2015-12-17 18:02
< >
分享
最佳答案
0

解决了!但是剑走偏锋,定义了一个信息队列,并实现入列和出列。在创建winfrom时,创建一个信息写入线程,定时轮询信息队列中是否存在信息,存在就输出。在其他py中,直接调用入列就可以实现。上面出错的原理明白,但是不知怎么解决。大概是在同一个py中当前能找到文本框的对象,但是在其他py中,单纯只是调用了输出信息的方法,但是没用创建文本框对象。想过直接找到该文本框对象,但是不知道怎样实现。如有哪位大神,有较好的解决方法,请指点,指点!

瀞默 | 初学一级 |园豆:149 | 2015-12-18 17:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册