cc1e 发表于 2014-05-15 21:05

python Tk 输出问题请教

Hi all:
python新手,想写一个基于Tk的图形界面东东。主要功能是查询大酒神(dota- -)优酷视频更新情况的,现在遇到一点问题,想请教一下各位大大。。运行环境:win7IDE,源码图如下:

Pyinstaller打包成exe后,运行正常,但是单机button之后,输出是由一个py解释器生成的,我的目标是想输出在Tk的界面中。。。
运行如下:


请问:我该如何修改呢?或者我该查哪个方面的资料呢?

先谢啦

cc1e 发表于 2014-05-15 21:08

# -*- coding: utf-8 -*-
import urllib
from Tkinter import *
import re
def getupdates():
    url = 'http://i.youku.com/u/UMzE2OTY2NjUy'
    data = urllib.urlopen(url)
    num_pattern = 'videos">.(?P<num>\d{2,4}).'
    time_pattern = 'c_time="(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}.\d{2}.\d{2})'
    video_name_pattern = 'replace="false"'
    for line in data.readlines():
      line = line.strip().decode('utf8')
      try:
            num = re.search(num_pattern,line)
            time = re.search(time_pattern,line)
            video = re.search(video_name_pattern,line)
            if num:
                b = int(num.group('num'))
                print u'大酒神共有%d个视频' % b
            elif time:
                print time.group('time')
            elif video:
                a = line.find('title')
                print line   
      except:
            pass
      
tk = Tk()
tk.title('09 dota update')
tk.geometry('600x600')
Label(tk,text = 'this is used for collecting 09 dota video updates').pack()
Button(tk,text = 'get the result',command = getupdates).pack()
tk.mainloop()

ssfjhh 发表于 2014-05-16 02:13

用Label即可,这段代码是python3的。# -*- coding: utf-8 -*-
import urllib.request
from tkinter import *
import re
def getupdates():
    url = 'http://i.youku.com/u/UMzE2OTY2NjUy'
    data = urllib.request.urlopen(url)
    num_pattern = 'videos">.(?P<num>\d{2,4}).'
    time_pattern = 'c_time="(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}.\d{2}.\d{2})'
    video_name_pattern = 'replace="false"'
    for line in data.readlines():
      line = line.strip().decode('utf8')
      try:
            num = re.search(num_pattern,line)
            time = re.search(time_pattern,line)
            video = re.search(video_name_pattern,line)
            if num:
                b = int(num.group('num'))
                Label(tk,text = '大酒神共有%d个视频'%b).pack()
            elif time:
                Label(tk, text = time.group('time')).pack()
            elif video:
                a = line.find('title')
                Label(tk, text = line).pack()
      except:
            pass

tk = Tk()
tk.title('09 dota update')
tk.geometry('600x600')
Label(tk,text = 'this is used for collecting 09 dota video updates').pack()
Button(tk,text = 'get the result',command = getupdates).pack()
tk.mainloop()

b4and5 发表于 2014-05-16 10:51

不知道大酒神看到此贴会滋生出如何的情愫来....

cc1e 发表于 2014-05-16 10:55

非常感谢!已经解决啦~~~:D 回复 3# ssfjhh


   

cc1e 发表于 2014-05-16 10:57

哈哈,我是9神脑残粉回复 4# b4and5


   

y2k_connect 发表于 2014-05-16 11:00

1. 在主循环的Button后面,增加一个Text控件。txt = Text(tk)
txt.pack()2. 在函数getupdates()中,print改为:txt.insert(END, '大酒神共有%d个视频' % b)
...
txt.update()
页: [1]
查看完整版本: python Tk 输出问题请教