2

I have been working on a GUI timer where time has to be given in the parameter. But the time can vary so I want to give the input from a text file.

The code is taken from the 1st answer.

Making a countdown timer with Python and Tkinter?

What I have tried:

f = open('f.txt', 'r')
data = f.read()     
f.close()               
print data  
s = 'data'
self.countdown(s)

The f.txt file has a number 10.

But not working due to datatype and getting the error below:

Traceback (most recent call last):
File "new.py", line 33, in <module>
app = ExampleApp()
File "new.py", line 15, in __init__
self.countdown(s)
File "new.py", line 28, in countdown
self.label.configure(text="%d" % self.remaining)
TypeError: %d format: a number is required, not str

Any help would be appreciated.

  • 1
    Copy paste the actual stacktrace please. The entire stack trace, in a code block and not a quote block :) – Torxed Mar 13 '19 at 19:04
  • `%d` means that you will use digit variable, and your `data` is a string. You need to pars it to `int`. – Karls Mar 13 '19 at 19:04
  • I have edited the post as "Torxed" asked. – damon salvator Mar 13 '19 at 20:06
  • Im not sure what you are trying to do, are you trying to pass the value in the file to the countdown function? What is your 's' variable suppose to contain? Coz right now it contains the string "data". If you want s to just have 10, use s=data (without the quotes). – V_stack Mar 13 '19 at 19:13

1 Answers1

3

When you use '' you are giving a string, not the variable.
Also, you probably want to read the value as a number. This is probably what you want:

s = int(data)
asergio
  • 81
  • 7
  • Finally, I am thanking all the answers. Especially what "asergio" replied, worked like a charm. – damon salvator Mar 13 '19 at 20:12
  • @damonsalvator, don't forget to read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) and consider [accepting an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) to close this question. – chickity china chinese chicken Mar 13 '19 at 20:19