Chinaunix

标题: python Tk 输出问题请教 [打印本页]

作者: 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
  1. # -*- coding: utf-8 -*-
  2. import urllib
  3. from Tkinter import *
  4. import re
  5. def getupdates():
  6.     url = 'http://i.youku.com/u/UMzE2OTY2NjUy'
  7.     data = urllib.urlopen(url)
  8.     num_pattern = 'videos">.(?P<num>\d{2,4}).'
  9.     time_pattern = 'c_time="(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}.\d{2}.\d{2})'
  10.     video_name_pattern = 'replace="false"'
  11.     for line in data.readlines():
  12.         line = line.strip().decode('utf8')
  13.         try:
  14.             num = re.search(num_pattern,line)
  15.             time = re.search(time_pattern,line)
  16.             video = re.search(video_name_pattern,line)
  17.             if num:
  18.                 b = int(num.group('num'))
  19.                 print u'大酒神共有%d个视频' % b
  20.             elif time:
  21.                 print time.group('time')  
  22.             elif video:
  23.                 a = line.find('title')
  24.                 print line[a+7:-3]   
  25.         except:
  26.             pass
  27.         
  28. tk = Tk()
  29. tk.title('09 dota update')
  30. tk.geometry('600x600')
  31. Label(tk,text = 'this is used for collecting 09 dota video updates').pack()
  32. Button(tk,text = 'get the result',command = getupdates).pack()
  33. tk.mainloop()
复制代码

作者: ssfjhh    时间: 2014-05-16 02:13
用Label即可,这段代码是python3的。
  1. # -*- coding: utf-8 -*-
  2. import urllib.request
  3. from tkinter import *
  4. import re
  5. def getupdates():
  6.     url = 'http://i.youku.com/u/UMzE2OTY2NjUy'
  7.     data = urllib.request.urlopen(url)
  8.     num_pattern = 'videos">.(?P<num>\d{2,4}).'
  9.     time_pattern = 'c_time="(?P<time>\d{4}-\d{2}-\d{2}\s\d{2}.\d{2}.\d{2})'
  10.     video_name_pattern = 'replace="false"'
  11.     for line in data.readlines():
  12.         line = line.strip().decode('utf8')
  13.         try:
  14.             num = re.search(num_pattern,line)
  15.             time = re.search(time_pattern,line)
  16.             video = re.search(video_name_pattern,line)
  17.             if num:
  18.                 b = int(num.group('num'))
  19.                 Label(tk,text = '大酒神共有%d个视频'%b).pack()
  20.             elif time:
  21.                 Label(tk, text = time.group('time')).pack()
  22.             elif video:
  23.                 a = line.find('title')
  24.                 Label(tk, text = line[a+7:-3]).pack()
  25.         except:
  26.             pass

  27. tk = Tk()
  28. tk.title('09 dota update')
  29. tk.geometry('600x600')
  30. Label(tk,text = 'this is used for collecting 09 dota video updates').pack()
  31. Button(tk,text = 'get the result',command = getupdates).pack()
  32. tk.mainloop()
复制代码

作者: b4and5    时间: 2014-05-16 10:51
不知道大酒神看到此贴会滋生出如何的情愫来....
作者: cc1e    时间: 2014-05-16 10:55
非常感谢!已经解决啦~~~ 回复 3# ssfjhh


   
作者: cc1e    时间: 2014-05-16 10:57
哈哈,我是9神脑残粉回复 4# b4and5


   
作者: y2k_connect    时间: 2014-05-16 11:00
1. 在主循环的Button后面,增加一个Text控件。
  1. txt = Text(tk)
  2. txt.pack()
复制代码
2. 在函数getupdates()中,print改为:
  1. txt.insert(END, '大酒神共有%d个视频' % b)
  2. ...
  3. txt.update()
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2