0

I am working on a small project to help me consolidate my learning of python. After endless trial with global variables, and returns; and searching I have decided to post here. Basically when the program is running, the user can select a .txt file from their computer and then the directory is recorded through the findFile() function specifically the my_label variable. I want to use the string stored in my_label to place it in the editFile function specifically the line: my_file = open("File location goes here","a+") in which it would look like my_file = open(my_label,"a+"). If anyone could provide help I would greatly appreciate it.

root = tk.Tk()

canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()

entry1 = tk.Entry (root) 
canvas1.create_window(200, 140, window=entry1)

def editFile ():

    x1 = entry1.get()

    label1 = tk.Label(root, text=x1 )
    canvas1.create_window(200, 230, window=label1)

    my_file = open("File location goes here","a+")

    my_file.read()

    my_file.write("\n" + x1)

    my_file.close()

def findFile ():
    root.filename = filedialog.askopenfilename(initialdir="C:\\Users\\mypc", 
title="Select A File", filetypes=(("txt files", "*.txt"),("All Files", "*.*"))) 
    my_label = Label(root, text=root.filename).pack() 
    canvas1.create_window(200, 290, window=my_label)

button1 = tk.Button(text='Enter Text', command=editFile)
canvas1.create_window(200, 180, window=button1)

button2 = tk.Button(text='Exit', command=root.destroy)
canvas1.create_window(200, 250, window=button2)

button3 = tk.Button(text='Find File', command=findFile)
canvas1.create_window(200, 270, window=button3)

root.mainloop()
  • 1
    in `my_label` you don't store any string nor `Label` but `None` because `pack()` always returns `None`. You have to do it in two steps `my_label = tk.Label()` and `my_label.pack()`. But you have your text in `root.filename` so use it in `editFile`. – furas Nov 24 '19 at 16:17
  • ***"After endless trial with global variables, and returns"***: Read [Best way to structure a tkinter application](https://stackoverflow.com/a/17470842/7414759) to get rid of using `global`. – stovfl Nov 24 '19 at 16:20
  • if you put widget (`my_label') on Canvas then you don't need `pack()`/`grid()`/`place()` – furas Nov 24 '19 at 16:21

1 Answers1

0

In findFile() you should use global filename to put value in global variable filename instead of creating local one.

In editFile() you don't have to use global because you only read value from variable.

But it is good to set default value at start filename = '' so you can check if filename was selected, and it will not raise error "variable doesn't exist".


BTW: at start I created labels without text and later I change text in existing labels instead of creating label again and again when different files will be selected.


import tkinter as tk
from tkinter import filedialog

def editFile():
    #global filename # doesn't need to get value from global variable

    print(filename)

    text = entry1.get()

    # update text in existing `Label`
    label_text['text'] = text

    if filename: # dont't write if filename is not selected
        my_file = open(filename, "a")
        my_file.write("\n" + text)
        my_file.close()
    else:
        print('no filename')

def findFile():
    global filename # inform function to put value in global variable instead of creating local one

    filename = filedialog.askopenfilename(
        initialdir="C:\\Users\\mypc", 
        title="Select A File",
        filetypes=(("txt files", "*.txt"), ("All Files", "*.*"))
    )

    # update text in existing `Label`
    label_filename['text'] = filename

# --- main ---

filename = '' # (global variable) default value at start

root = tk.Tk()

canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()

entry1 = tk.Entry (root) 
canvas1.create_window(200, 140, window=entry1)

button1 = tk.Button(text='Enter Text', command=editFile)
canvas1.create_window(200, 180, window=button1)

button2 = tk.Button(text='Exit', command=root.destroy)
canvas1.create_window(200, 250, window=button2)

button3 = tk.Button(text='Find File', command=findFile)
canvas1.create_window(200, 270, window=button3)

# create only once and without text
label_filename = tk.Label(root)
canvas1.create_window(200, 290, window=label_filename)

# create only once and without text
label_text = tk.Label(root)
canvas1.create_window(200, 230, window=label_text)

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you furas you helped out a bunch. It works well and you helped me with formatting and better understanding the code you wrote. =) – BioFluorescent277 Nov 25 '19 at 08:03