簡體   English   中英

無法弄清楚如何在另一個函數中使用一個函數中的變量 - Python

[英]Can't figure out how to use a variable from one function in another - Python

我正在做一個小項目來幫助我鞏固我對 Python 的學習。 經過對全局變量的無休止的試驗,並返回; 和搜索我決定在這里發帖。 基本上當程序運行時,用戶可以從他們的計算機中選擇一個 .txt 文件,然后通過 findFile() 函數特別是 my_label 變量記錄目錄。 我想使用存儲在 my_label 中的字符串將它放在 editFile 函數中,特別是以下行: my_file = open("File location go here","a+") 其中它看起來像 my_file = open(my_label,"a+" )。 如果有人可以提供幫助,我將不勝感激。

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()

findFile()您應該使用global filename findFile()值放入全局變量filename而不是創建本地變量。

editFile()您不必使用global因為您只從變量中讀取值。

但是最好在開始時設置默認值filename = ''這樣您就可以檢查是否選擇了文件名,並且不會引發錯誤"variable doesn't exist"


順便說一句:一開始我創建了沒有文本的標簽,后來我更改了現有標簽中的文本,而不是在選擇不同文件時一次又一次地創建標簽。


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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM