脚本可以在Pycharm中运行,但是在cmd中运行时,没有任何反应

时间:2017-06-02 16:01:18

标签: python

编写一个python脚本,它可以在pycharm中成功运行,但是当它在cmd中运行时,它不起作用,没有报告任何错误。

当它在Pycharm中运行时,它会显示预期的输出:

截屏1

当它在cmd中运行时,它显示一个空白的Tk窗口:

截屏2

当它在cmd中运行时,它只有gui,而不是任何内容。

这是代码:

#-*-coding:utf-8-*-
from bs4 import BeautifulSoup
import requests,codecs,urllib,sys
import codecs
from Tkinter import  *
import time,datetime,winsound


def code(text):
    text='外贸'
    url_gbk = urllib.quote(text.decode(sys.stdin.encoding).encode('gbk'))

    return url_gbk

def get(url):
    try:
        response = requests.get(url)
        return response.text
    except EOFError:
    print 'error'

def makesoup(response):
    list=[]
    soup = BeautifulSoup(response,'lxml')
    soup = soup.find_all(attrs='li',class_='seaList13')
    for i in soup:
         list.append(i.text)
    return list


def get_yao_xian_shi_de_( i ):
    yi_cun_zai=[]
    yao_xian_shi_de_ = []
    txt = codecs.open(r'D:\yuyao\.idea\txt.txt','a+','utf-8')

yi_cun_zai = [k.strip() for k in txt]
txt.close()
txt = codecs.open(r'D:\yuyao\.idea\txt.txt','a+','utf-8')

text    = '外贸'
text    = code(text)
url     = 'http://www.yyrc.com/job/?     JobType=0&WorkPlace=0&Trade=0&Property=0&' \
          'JobProperty=0&Degree=0&WorkYears=0&Sex=0&MonthPay=0&' \
          'PublishDate=0&Key='+text+'&Orderid=0&Styleid=2&PageNo='+str(i)
response = get(url)
list_1=makesoup(response)
list_1= list(set(list_1))

for j in list_1:
        if(j in yi_cun_zai):
            pass
        else:
            print j
            yao_xian_shi_de_.append(j)
            txt.write(j+'\r\n')
txt.close()
return yao_xian_shi_de_

def on_go():
    root = Tk()
    t    = Text(root)
    t.pack()
    for i in range(5):
        xian_shi_wo  =   get_yao_xian_shi_de_(i)
        for j in xian_shi_wo:
            winsound.Beep(500,500)
            t.insert(INSERT,j+'        ')
            t.insert(INSERT,str(time.ctime())+'\n\r')
            t.mark_set('insert', 1.0)
            t.update()
            time.sleep(0.1)
    t.mainloop()

if __name__ == '__main__':
    on_go()

`

0 个答案:

没有答案