I don't know what can I do, so I'm asking for help. When I click that "submitting_button" error occur "write() argument must be str not stringvar"
import tkinter as tk
from tkinter import ttk, Label, Button, Entry, Place, Pack, StringVar
class Datasaver:
def __init__(self, GUI,):
self.GUI = GUI
GUI.title("Dataver")
self.label = Label(GUI, text="More comfortable method for saving data!",font=("Arial", 20))
self.label.place(relx = 0.1, rely = 0.5, anchor = 'sw')
self.submitting_button = Button(GUI, text="submit", command=self.submitting_button)
self.submitting_button.place(relx = 0.9, rely = 0.9, anchor = 'sw')
self.close_button = Button(GUI, text="Exit", command=GUI.quit)
self.close_button.place(relx = 0.9, rely = 1.0, anchor = 'sw')
self.butt = Button(GUI, text="create", command=self.create)
self.butt.place(relx = 0.9, rely = 0.8, anchor = 'sw')
self.labelel = Label(GUI, text="Username:")
self.labelel.place(relx = 0.0, rely = 0.8, anchor = 'sw')
self.labelel = Label(GUI, text="Password:")
self.labelel.place(relx = 0.0, rely = 0.9, anchor = 'sw')
self.labelel = Label(GUI, text="E-mail:")
self.labelel.place(relx = 0.0, rely = 1.0, anchor = 'sw')
self.entry = Entry(GUI, textvariable = name_variable, show="*", font=("Arial", 10))
self.entry.place(relx = 0.2, rely = 0.9, anchor = 'sw')
self.entry2 = Entry(GUI, textvariable = password_var, font=("Arial", 10))
self.entry2.place(relx = 0.2, rely = 1.0, anchor='sw')
self.entry3 = Entry(GUI, textvariable = Email_var, font=("Arial", 10))
self.entry3.place(relx = 0.2, rely = 0.8, anchor='sw')
def create(self):
f = open("myfile.txt", "x")
def submitting_button(self):
file = open("myfile.txt", "w", encoding="utf8")
file.write(name_variable)
file.write(password_var)
file.write(Email_var)
file.close()
name=name_variable.get()
password=password_var.get()
Email=Email_var.get()
print("Nickname: " name)
print("Password: " password)
print("Email: " Email)
name_variable.set("")
password_var.set("")
Email_var.set("")
root = tk.Tk()
root.resizable(False, False)
root.geometry('600x350')
name_variable=StringVar()
password_var=StringVar()
Email_var=StringVar()
my_gui = Datasaver(root)
root.mainloop()
CodePudding user response:
To extend on Barmar's answer:
StringVar
is a Tkinter class that helps manage the values of a widget. As such a StringVar
object holds a string (in it's value
attribute) but is not a string itself.
file.write()
on the other hand expects a string.
As explained here Access StringVar() as a normal string in python you can get the value of the StringVar
variable by using the get()
method.
To make it short, all you need to do is change the following lines:
def submitting_button(self):
# ...
file.write(name_variable.get())
file.write(password_var.get())
file.write(Email_var.get())
# ...