1

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

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
Shekhar Samanta
  • 875
  • 2
  • 12
  • 25
  • `root.mainloop()` appearing as anything other than the last line of code is a bad sign. Nothing after `mainloop()` runs until the application is destroyed. – TigerhawkT3 Oct 30 '15 at 12:33
  • Are you aware that `Tk().withdraw()` is creating a _second_ root window? – Bryan Oakley Oct 30 '15 at 12:41
  • either removing root from ( root.mainloop()) or removing Tk().withdraw() isn't solving the problem. Something in the (If else) condition sys.exit() , The python shell exits with exit code 0 , but Tk window doesn't closes – Shekhar Samanta Oct 30 '15 at 12:47
  • @ShekharSamanta: if you call `sys.exit()`, the window _must_ close. I think you might be misinterpreting what you are seeing. When I run your code, enter a search term and city, it prompts me for a file, and then the program exits, just as I would expect it to do. – Bryan Oakley Oct 30 '15 at 12:52
  • @Bryan Oakley , Thank you for testing the code , Try leaving one of the fields blank either Search_string or City , then click Submit , it will exit python shell, but the TK window will remain intact & go not responding . – Shekhar Samanta Oct 30 '15 at 12:56
  • @Bryan Oakley, I added a countdown function, you could see how the tk window remains intact & shows not responding when (if or elif ) is true. Is there anyway the tk window goes away after clicking submit button . – Shekhar Samanta Oct 30 '15 at 13:20
  • You've changed the nature of the question by adding the countdown timer. Without it, even with one field blank your program exits as I would expect it. Your countdown timer calls sleep in a loop, which will cause the UI to freeze. – Bryan Oakley Oct 30 '15 at 13:34

1 Answers1

0

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.

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

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 countdown(delay): if delay == 0: sys.exit() print "program will exit in %s seconds" % delay root.after(1000, countdown, delay-1)

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.

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

  1. tkinter: how to use after method (stackoverflow)
  2. http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method
Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685