Python Tk windows挂了 [英] Python Tk windows hangs up

查看:83
本文介绍了Python Tk windows挂了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码基本上将 2 个用户输入作为字符串 &稍后使用它们形成 URL

This code basically takes 2 user inputs as string & later use them to form URL

运行此代码后,如果(if 或 elif)条件为 False,TK 窗口将挂起(冻结),这是代码.

After i run this code , The TK window hangs up(freezes) if (if or elif) condition is False, here is the code .

有人请帮忙.请看下面的代码.

Somebody please help on the same. Please see below code.

#!/usr/bin/env python
from Tkinter import *
import requests, csv, time, json, tkFileDialog
from lxml import html, etree
from tkFileDialog import askopenfilename

def countdown(p,q):
    i=p
    j=q
    k=0
    while True:
        if(j==-1):
            j=59
            i-=1
        if(j > 9):  
            print "\r"+str(k)+str(i)+":"+str(j),
        else:
            print "\r"+str(k)+str(i)+":"+str(k)+str(j),
        time.sleep(1)
        j -= 1
        if(i==0 and j==-1):
            break
    if(i==0 and j==-1):
        print "\rGoodbye!"
        time.sleep(1)

links = []

root = Tk()
root.geometry('300x130')

label1 = Label( root, text="Please Enter Search Term")
E1 = Entry(root, bd =5)

label2 = Label( root, text="Please enter City")
E2 = Entry(root, bd =5)

def quit(self):
    self.root.destroy()

def getData():
    a = 'try again'
    global Search_string, City
    Search_string = E1.get();
    City = E2.get();
    if Search_string == '':
        print 'Please enter [mandatory field] Search Term - (%s)' % a
        print 'Program will exit in 10 seconds'
        countdown(0,10)
        sys.exit()
    elif City == '':
        print 'Please enter [mandatory field] City also - (%s)' % a
        print 'Program will exit in 10 seconds'
        countdown(0,10)
        sys.exit()
    else:
        print Search_string,'\n',City
    root.destroy()

submit = Button(root, text ="Submit", command = getData)

label1.pack()
E1.pack()
label2.pack()
E2.pack()

submit.pack(side=BOTTOM)
mainloop()

Search_string = Search_string.replace(' ','+')
partial_link_1 = ("https://www.google.com/search?q=");partial_link_2 = '&num=10'

Tk().withdraw()
csvfile = tkFileDialog.asksaveasfile(mode='w', defaultextension=".csv")

i = 0
while i < 40:
    url = partial_link_1+Search_string+'+'+'in'+'+'+City+partial_link_2+'&start='+str(i)
    i+=10
    links.append(url)

for i in links: print "Links = %r"%i

推荐答案

tkinter(和大多数 GUI 工具包)的工作方式是它们依赖于源源不断的事件.有鼠标和键盘事件,但也有刷新显示和其他低级功能的事件.

The way tkinter (and most GUI toolkits) work, is that they rely on a steady stream of events. There are mouse and keyboard events, but there are also events for refreshing the display and other lower level functions.

当您在循环中调用 sleep 时,事件循环无法处理这些事件,这会导致您的 GUI 冻结.

When you call sleep in a loop, the event loop is not able to process these events, which causes your GUI to freeze.

如果你想展示一个倒数计时器,无限循环不是这样做的方式.Tkinter 小部件有一个名为 after 的方法,您可以使用它来安排将来要调用的函数.

If you want to present a count-down timer, an infinite loop is not the way to do it. Tkinter widgets have a method named after which you can use to schedule a function to be called in the future.

例如,您可以采用一个接受数字的函数.如果数字为零,它将退出.如果它不是零,它会递减数字,然后在一秒钟内再次调用它自己.

For example, you can take a function that accepts a number. If the number is zero, it will exit. If it's not zero, it decrements the number and then causes itself to be called again in one second.

例如,它可能看起来像这样:

For example, it might look something like this:

def 倒计时(延迟):如果延迟== 0:系统退出()打印程序将在 %s 秒后退出"% 延迟root.after(1000, 倒计时, delay-1)

def countdown(delay): if delay == 0: sys.exit() print "program will exit in %s seconds" % delay root.after(1000, countdown, delay-1)

当你像 delay(10) 一样调用它时,它会安排 delay 在一秒内再次调用,但以 9 作为参数.它将继续递减该值,直到达到零,然后退出.

When you call it like delay(10), it will schedule delay to be called again in one second, but with 9 as an argument. It will continue to decrement the value until it hits zero, at which point it will exit.

有关使用 after 的更多信息,这里有一些不错的资源:

For more information on using after, here's a couple of good resources:

  1. tkinter:如何使用 after 方法(stackoverflow)
  2. http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method
  1. tkinter: how to use after method (stackoverflow)
  2. http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method

这篇关于Python Tk windows挂了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆