Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法理解如何在另一个函数中使用来自一个函数的变量-Python_Python_Function_File_Variables_Tkinter - Fatal编程技术网

无法理解如何在另一个函数中使用来自一个函数的变量-Python

无法理解如何在另一个函数中使用来自一个函数的变量-Python,python,function,file,variables,tkinter,Python,Function,File,Variables,Tkinter,我正在做一个小项目,帮助我巩固对python的学习。用全局变量进行无休止的尝试后,返回;我决定在这里发布搜索。基本上,当程序运行时,用户可以从计算机中选择一个.txt文件,然后通过findFile函数(特别是my_label变量)记录目录。我想使用my_label中存储的字符串将其放在editFile函数中,具体来说是这样一行:my_file=openFile location在这里,a+看起来像my_file=openmy_label,a+。如果有人能提供帮助,我将不胜感激 root = tk

我正在做一个小项目,帮助我巩固对python的学习。用全局变量进行无休止的尝试后,返回;我决定在这里发布搜索。基本上,当程序运行时,用户可以从计算机中选择一个.txt文件,然后通过findFile函数(特别是my_label变量)记录目录。我想使用my_label中存储的字符串将其放在editFile函数中,具体来说是这样一行:my_file=openFile location在这里,a+看起来像my_file=openmy_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中,应该使用全局文件名将值放入全局变量文件名中,而不是创建本地变量文件名

在editFile中,您不必使用全局变量,因为您只从变量中读取值

但最好在start filename=处设置默认值,这样您就可以检查是否选择了filename,并且不会引发错误,因为变量不存在

顺便说一句:一开始我创建了没有文本的标签,后来当选择不同的文件时,我改变了现有标签中的文本,而不是一次又一次地创建标签


在my_标签中,您不存储任何字符串或标签,但不存储任何字符串或标签,因为pack总是返回None。您必须分两个步骤完成此操作:my_label=tk.label和my_label.pack。但是你的文本在root.filename中,所以在editFile中使用它。在对全局变量进行无休止的尝试后,返回:Read以摆脱使用全局变量。如果你将widget my_label'放在画布上,那么你就不需要pack/grid/place`谢谢你,因为你帮了很多忙。它工作得很好,您帮助我格式化并更好地理解您编写的代码=
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()