Home > other >  How to make a button with the name of a file and also call that file?
How to make a button with the name of a file and also call that file?

Time:02-11

So I am trying to make a sort of journal program where you can create an entry that is then saved as a text file. I currently have it set up so you can recall an entry by typing in the file name and clicking load, but I want to make a list of buttons on the right side that has all of the file names and then will load the respective file when clicked, any suggestions?

Here is my code:

from cProfile import label
from cgitb import text
from email.quoprimime import quote
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter
from traceback import print_tb
import os

from pip import main


def open_main():

    #instantiate main screen
    mainscreen = tk.Tk()
    mainscreen.state("zoomed")
    mainscreen.title("Welcome")

    #file name text box
    filename = tk.Entry(mainscreen)
    filename.place(relx=.5, rely=.1, anchor=CENTER)

    #save entry function
    def save_entry():
        savefile = open("%s .txt" % filename.get(), "w ")
        savefile.write(T.get("1.0","end"))
        savefile.close
        refresh()

    #load entry function
    def loadentry():
        loadentry = open("%s .txt" % filename.get(), "r")
        quote = loadentry.readlines()
        T.delete("1.0", END)
        T.insert(END, quote)

    #create frame to place main text box
    mainframe = tkinter.Frame(mainscreen)
    mainframe.place(relwidth=.65, relheight=.75, relx=.05, rely=.5, anchor=W)

    #label the file name text box
    tk.Label(mainscreen, text="Make an Entry:").place(relx=.5, rely=.035, anchor=CENTER)
    tk.Label(mainscreen, text="Date: MMDDYYYY").place(relx=.5, rely=.07, anchor=CENTER)

    #create main text box within the main frame
    S = Scrollbar(mainframe)
    T = Text(mainframe)
    S.pack(side=RIGHT, fill=Y)
    T.pack(side=LEFT, expand=True, fill=BOTH)
    S.config(command=T.yview)
    T.config(yscrollcommand=S.set)

    #create second frame next to main frame to hold buttons
    sideframe = tkinter.Frame(mainscreen)
    sideframe.place(relwidth=.2, relheight=.75, relx=.7, rely=.5, anchor=W)
    side_bar = Scrollbar(sideframe)
    side_box = Text(sideframe)
    side_bar.pack(side=RIGHT, fill=Y)
    side_box.pack(side=LEFT, expand=True, fill=BOTH)


    #create and load buttons
    def loadbutton(item):
        bfilename = item
        bfileentry = open(bfilename, "r")
        bquote = bfileentry.readlines()
        T.delete("1.0",END)
        T.insert(END,bquote)

    #add buttons to box initially
    entry_initate = [f for f in os.listdir(os.getcwd()) if f.endswith('.txt')]

    for item in entry_initate:
        mybutton = Button(side_box, text=item, command = lambda m = item: loadbutton(item))
        mybutton.pack(fill=BOTH)

    #refresh buttons when a new entry is saved
    def refresh():
    
        entry_raw = [f for f in os.listdir(os.getcwd()) if f.endswith('.txt')]

    for item in entry_raw:
            mybutton = Button(side_box, text=item, command = lambda m = item: loadbutton(item))
            mybutton.pack(fill=BOTH)

    list = side_box.slaves()
    for l in list:
        l.destroy()

    for item in entry_raw:
        mybutton = Button(side_box, text=item, command = lambda m = item: loadbutton(item))
        mybutton.pack(fill=BOTH)

    

    #Save and load entry buttons
    Button(mainscreen, text="Save Entry", command=save_entry).place(relx=.5, rely=.9, anchor=CENTER)
    Button(mainscreen, text="Load Entry", command=loadentry).place(relx=.5, rely=.95, anchor=CENTER)


mainscreen.mainloop()

I currently just have the side box just commented out, it was originally just a text box that had the file names listed in it.

Sorry if its a little messy, im still pretty new to python.

CodePudding user response:

You should use variable loadfileb instead of button_press on .readlines() inside loadbutton(). Also use read() instead of readlines() and you need to insert the read content into text box.

    def loadbutton(button_press):
        with open(button_press, "r") as loadfileb:
            quote = loadfileb.read()
        T.delete('1.0', 'end')
        T.insert('end', quote)

    for item in entry_raw:
        tk.Button(sideframe, text=item, command=lambda m=item: loadbutton(m)).pack()

Note that there are different way to initiate widgets, like tk.Entry(...), tkinter.Frame(...) and Button(...) in your code. So I think you have imported tkinter like below:

import tkinter
from tkinter import *
import tkinter as tk

Recommend to use import tkinter as tk only.

  • Related