I created a working sign-out script using Python3 / tkinter. To finish it, I am trying to transfer the name of a button to a new window, then to a txt file.
In my sign-out program, when the person clicks on a category button, it opens a corresponding items.txt file and then creates a new window with a button for each item in the file. When one of the item button is clicked, a sign-out window is created with an Entry box for their name and a ‘Submit’ button. When the submit button is pressed, I want it to write the item name, person’s name, date, and time to a sign-out text file.
The answers I have found so far talk about a field of buttons that are created (like in Minesweeper).
Since these are no static buttons, I don’t know how to determine which button was pressed and what item name was assigned to that button. The names and the number of items in the file changes.
I tried to make the ‘name’ global in the button_click_pk
section but it only returns the name of the last button created. I don’t know if it’s related, but it incorrectly puts the name on a different line.
Item Five
Jane Smith 2016-12-19 10:30:53
I want it to show on one line:
Item Five Jane Smith 2016-12-12 13:30:53
How do you tie a name to a button created in a for loop from a txt file?
class Writefiles:
def __init__(self):
global win3
win3 = Tk()
self.VarEnt = StringVar()
self.lab = Label(win3, text = "Signature")
self.lab.grid(padx = 10, pady = 10)
self.ent = Entry(win3, font = ('Lucida Handwriting', 10), textvariable = self.VarEnt, bd = 5, width = 45)
self.ent.focus()
self.ent.grid(padx = 10, pady = 10)
self.btn = Button(win3, text = 'Submit', width = 10, height = 2, background = 'gold', command = self.write_to_file)
self.btn.grid(padx = 10, pady = 10)
def write_to_file(self):
date = datetime.now().strftime(' %Y-%m-%d %H:%M:%S')
with open('sig.txt', 'a') as f:
f.write(name + self.ent.get() + date + '\n')
f.close()
win3.destroy()
def button_click_pk(): # creates the buttons from text file
global name
win2 = Tk()
file = open('item.txt', 'r')
rw = 0
cl = 0
for name in file:
b1 = Button(win2, text = name, wraplength = 100, width = 18, height = 5, background = 'gold', command = Writefiles)
b1.grid(row = rw, column = cl, padx = 11, pady = 13)
cl += 1
if cl == 4:
cl = 0
rw += 1