Is there a way to restore the same previous session attributes in tkinter ? for example: having one button to restore the previous session attributes
Is there a way to set inputtxt to the value?
def restore():
file1 = open('user.txt', 'r')
Lines = file1.readlines()
unverified_file = Lines[0]
**inputtxt1.set(unverified_file)**
AttributeError: 'Text' object has no attribute 'set'
Looking at your updated code, you need insert not set. But first delete the entry -
def restore():
file1 = open('user.txt', 'r')
Lines = file1.readlines()
unverified_file = Lines[0]
inputtxt1.delete(0,'end')
inputtxt1.insert('end',unverified_file)
Also, take a look at this example -
import tkinter as tk
root = tk.Tk()
def save():
a = entry1.get()
b = entry2.get()
with open('1.txt','w') as f:
f.write(a)
f.write('\n')
f.write(b)
def restore():
with open('1.txt','r') as f:
text = f.read().split('\n')
entry1.insert('end',text[0])
entry2.insert('end',text[1])
entry1 = tk.Entry(root)
entry1.pack()
entry2 = tk.Entry(root)
entry2.pack()
button1 = tk.Button(root,text='Save Results',command=save)
button1.pack()
button2 = tk.Button(root,text='Restore Previous Results',command=restore)
button2.pack()
root.mainloop()
Related
I cannot load a checkbox value from a file and have it correctly check the checkbox when the value is 1.
With the code below you can check a box and save it to a file. Clicking Load will load that value into a second variable, convert it to Int and create a checkbox linked to its value, but it will never generate already checked, even when the value of it's variable is 1 as shown by the print command. What am I doing wrong?
Window = Tk()
var = IntVar()
LoadVarTemp = 0
LoadVar = IntVar()
def SaveIt():
YesVar = var.get()
with open("config.txt", "w") as configfile:
configfile.write(str(YesVar))
def LoadIt():
with open("config.txt") as file:
content = file.readlines()
content = [x.strip() for x in content]
LoadVarTemp = content[0]
LoadVar = int(LoadVarTemp)
print(LoadVar)
Checkbutton(Window, text="Loaded", variable=LoadVar).grid(row=1)
YesCheck = Checkbutton(Window, text="Yes?", variable = var).grid(row=0)
SaveButton = Button(Window, text="Save", command=SaveIt).grid(row=2)
LoadButton = Button(Window, text="Load", command=LoadIt).grid(row=3)
Window.mainloop()
I was making a simple flashcard tkinter application and I've encountered a problem such that when I manage the subjects, only every second item is added to the file.
For example, say I went to the manage subject page and I wanted to add the subjects: subject1, subject2, subject3, subject4, ... subjectN.
The only subjects that would add to the file are the subjects with the odd endings, is there a fix for this?
The code responsible for this:
#subjects window
def managesubjectsF():
def addF():
f = open ('subjectlist.txt', 'a')
f.write(addsubjectE.get() + '\n')
f.close()
addsubjectE.delete('0', 'end')
subjectPage = Tk()
subjectPage.title('Add subject')
subjectPage.resizable(False, False)
subjectPage.configure(background = 'light blue')
subjectPage.geometry('260x70')
addsubjectE = Entry(subjectPage, width=23, font=('bold', 14))
addsubjectE.grid(column=1, row=1)
addS = Button(subjectPage, text='Add', font=('bold', 15), command = addF)
addS.grid(column=1, row=2, sticky = N)
Minimal example of implementation:
#modules
from tkinter import *
import os.path
#implementations
count=0
subjectlist = []
#main window details
root = Tk()
root.geometry('225x350')
#list of available subjects
choice = Listbox(root, font = ('bold', 15))
choice.grid(column=1, row=2)
if os.path.exists('./subjectlist.txt') == True:
with open('subjectlist.txt', 'r') as f:
for line in f:
count += 1
f.close()
f = open('subjectlist.txt', 'r')
for i in range(count):
choice.insert(END, (f.readline().strip()))
subjectlist.append(f.readline().strip())
f.close()
else:
f = open('subjectlist.txt', 'w')
f.close()
#subjects window
def managesubjectsF():
def addF():
f = open ('subjectlist.txt', 'a')
f.write(addsubjectE.get() + '\n')
f.close()
addsubjectE.delete('0', 'end')
subjectPage = Toplevel()
subjectPage.geometry('260x70')
addsubjectE = Entry(subjectPage, width=23, font=('bold', 14))
addsubjectE.grid(column=1, row=1)
addS = Button(subjectPage, text='Add', font=('bold', 15), command = addF)
addS.grid(column=1, row=2, sticky = N)
#buttons
addsubjectsB = Button(root, text = 'Manage subjects', font = ('bold', 15), command = managesubjectsF)
addsubjectsB.grid(column=1, row=3, sticky = N)
root.mainloop()
(moving comment to answer)
You are calling readline twice which is moving the file pointer and skipping subjects.
for i in range(count):
choice.insert(END, (f.readline().strip()))
subjectlist.append(f.readline().strip()) # reading next line
Try reading the value once then storing in both lists:
for i in range(count):
s = f.readline().strip() # read once
choice.insert(END, (s))
subjectlist.append(s)
I made a program with a List and this List is included in the Combobox. I want to write the List into a file in order to read the List again later, even after a restart of the program.
I tried this:
import tkinter as tk
from tkinter import ttk
import pickle
# window
win = tk.Tk()
win.title("menu")
List = []
newList = []
with open('data.txt', 'wb') as f:
pickle.dump(List, f)
with open('data.txt', 'rb') as f:
newList = pickle.load(f)
# button click event
def clickMe():
List.append(name.get())
numberChosen.configure(values=List)
# text box entry
ttk.Label(win, text="Eingabe:").grid(column=0, row=0)
name = tk.StringVar()
nameEntered = ttk.Entry(win, width=12, textvariable=name)
nameEntered.grid(column=0, row=1)
# button
action = ttk.Button(win, text="Enter", command=clickMe)
action.grid(column=2, row=1)
# drop down menu
ttk.Label(win, text="Auswahl:").grid(column=1, row=0)
number = tk.StringVar()
numberChosen = ttk.Combobox(win, width=12)
numberChosen['values'] = [List]
numberChosen.grid(column=1, row=1)
win.mainloop()
You just need to save the list to the file after mainloop, and load it at the start of the program.
with open('data.txt', 'rb') as file:
data = pickle.load(file)
...
win.mainloop()
with open('data.txt', 'wb') as file:
pickle.dump(data, file)
This will load the list at the start, and save it after the tk window closes.
In tkinter, python, I'm creating an info like text editor that uses StringVar() to display a file, also, with the use of entry and a button, I created a delete button, which deletes a certain line in a file. The problem I have is that the file does not delete the specified line and it does not update to show the file's contents. Here's my code:
from tkinter import *
root = Tk()
root.title("text")
root.geometry("700x700")
file = open(r"info.txt", "r")
lines = file.readlines()
file.close()
strd = StringVar()
def updatestr():
fileread = open(r"info.txt", "r")
lines2 = fileread.readlines()
strd.set("{}".format(lines2))
root.after(1, updatestr)
fileread.close()
updatestr()
lads = Label(root, textvariable=strd)
lads.pack()
blank = Label(root, text="")
blank.pack()
blank = Label(root, text="")
blank.pack()
blank = Label(root, text="")
blank.pack()
entry = Entry(root)
entry.configure(width=9)
entry.pack()
def DeleteEntry():
global file
file = open(r"info.txt", "w")
global lines
for line in lines:
if line!="{}".format(entry.get())+"\n":
file.write(line)
file.close()
confent = Button(root, text="Delete", command=DeleteEntry)
confent.configure(width=7)
confent.pack()
Not sure why this is happening, so I'd appreciate some help :)
Thanks
What I need is to make the view orders button to get the text from the Customer.txt file and set it inside a textfield i made.
#make order,cancel,view
from tkinter import *
import tkinter.messagebox
root = Tk()
file = open("Customer.txt", "w")
def textW():
outFile = open("Customer.txt", "wt")
def CancelOrder():
outFile=open("Customer.txt", "w")
outFile.write("")
tkinter.messagebox.showinfo("Cancel Order", "Your order has been canceled")
def ViewOrder():
outFile = open('Customer.txt', 'r')
test = outFile.read()
#tViewOrder.set(test)
print (test)
#test.set(tViewOrder)
#outFile.close()
def MakeOrder():
outFile=open("Customer.txt", "w")
outFile.write("" + tMakeOrder.get())
tkinter.messagebox.showinfo("Make Order", "Order has been placed. Thank you!")
#Labels
lMakeOrder = Label(root, text="Make an order")
lViewOrder = Label(root, text="View Order")
#TextFields
tMakeOrder = Entry(root)
tViewOrder = Entry(root, state="disabled")
#Buttons
bMakeOrder = Button(root, text="Make order",bg="black",fg="green", command=MakeOrder)
bCancelOrder = Button(root, text="Cancel order",bg="black",fg="green", command=CancelOrder)
bViewOrder = Button(root, text="View orders",bg="black",fg="green", command=ViewOrder)
#Position
lMakeOrder.grid(row=0)
lViewOrder.grid(row=1)
tMakeOrder.grid(row=0, column=2)
tViewOrder.grid(row=1, column=2)
bMakeOrder.grid(row=4)
bViewOrder.grid(row=4, column=2)
bCancelOrder.grid(row=4, column=4)
#Window stuff
root.title("Sky is a shit name service - Customer")
root.geometry("300x300")
root.mainloop()
You can put text inside your Entry by calling insert function on it.
MyEntry.insert(POSITION, TEXT)
Oh and one more thing. You can't insert anything in the entry if it's disabled.
So here is your modified function:
def ViewOrder():
outFile = open('Customer.txt', 'r')
test = outFile.read()
tViewOrder['state'] = 'normal'
tViewOrder.delete(0, 'end') #Remove everything before
tViewOrder.insert(0, test)
tViewOrder['state'] = 'disabled'
outFile.close()