SuperBeginner
SuperBeginner

Reputation: 1

GUIZERO: I want to use a pushbutton to save what's in a textbox to file. How do I do that?

I'm a super beginner with Python, so please be kind.

I am creating an app that should take in user input from text boxes, and then when the user presses the submit button this is saved in a text file.

I think the issue is that I'm not quite sure how to create the right function for the pushbutton command.

I would really appreciate if someone can code a simple app showing how to do this.

This is the code I have so far, but I get an error "TypeError: write() argument must be str, not TextBox".

from guizero import *
import os
cwd = os.getcwd()


# function for writing files
def save_file():
    with open(cwd+'/Desktop/File handling/newfile.txt','a') as f:
        f.write(userInput)

app = App("testing")

userInput = TextBox(app)
submit_button = PushButton(app, command=save_file, text="submit")

app.display()
`

Upvotes: 0

Views: 483

Answers (1)

SuperBeginner
SuperBeginner

Reputation: 1

I figured it out. Thanks :)

    from guizero import *
import os
cwd = os.getcwd()


# function for writing files
def save_file():
    with open(cwd+'/Desktop/File handling/newfile.txt','a') as f:
        f.write("First name:"+" "+first_name.value+"\n")

#the app
app = App("testing")


#text box
first_name = TextBox(app, width=30, grid=[2,4])

#submit button
box = Box(app)
submitbutton = PushButton(app, command=(save_file), text="Submit")

app.display()

Upvotes: 0

Related Questions