0

I'm currently trying to help a friend out with the same task that i have previously done, but im stuck. once the program is run and 'get tickets' is pressed this error message is displayed:

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Python34\lib\tkinter__init__.py", line 1533, in call return self.func(*args) File "C:\Users\harding\Documents\Oli\Homework\Year10\Computing\New folder\carpark 3.4 (1).py", line 21, in reg_output b.readlines(v.get()) TypeError: integer argument expected, got 'str'

code:

    from tkinter import *
    import time

    root = Tk()
    root.title('Car Park')

    v = StringVar()

   car_in=Label(root, text= "Please enter your reg number")
   car_in.grid(column=1, row=1)

   reg_input=Entry(root, textvariable =v)
   reg_input.grid(column=1, row=2)

   def reg_input():
     with open ('tickets.txt', 'w') as b:
        b.writelines(v.get())

   def reg_output():
     with open ('tickets.txt', 'r') as b:
        b.readlines(v.get())

   reg_input_but=Button(root, text='Submit', command=reg_input)
   reg_input_but.grid(column=1, row=4)

   reg_output_b=Button(root, text='Get Ticket', command=reg_output)
   reg_output_b.grid(column=1, row=5)

2 Answers 2

1

v is a Stringvar, therefore v.get() returns a string. b.readlines(lines) requires lines to be an integer because lines determines how many lines are supposed to be read. If you omit the argument it will read all lines.

Sign up to request clarification or add additional context in comments.

Comments

0

Looking at the error, function readlines expects integer arguments and v.get() returns a string, causing the error. What you can do is simply change b.readlines(v.get()) on line 21 to b.readlines() to fix the error.

Here's the edited code.

from tkinter import *
import time

root = Tk()
root.title('Car Park')

v = StringVar()

car_in=Label(root, text= "Please enter your reg number")
car_in.grid(column=1, row=1)

reg_input=Entry(root, textvariable =v)
reg_input.grid(column=1, row=2)

def reg_input():
    with open ('tickets.txt', 'w') as b:
        b.writelines(v.get())

def reg_output():
    with open ('tickets.txt', 'r') as b:
        b.readlines()

reg_input_but=Button(root, text='Submit', command=reg_input)
reg_input_but.grid(column=1, row=4)

reg_output_b=Button(root, text='Get Ticket', command=reg_output)
reg_output_b.grid(column=1, row=5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.