gpt4 book ai didi

python - python中变量的调用

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:38 24 4
gpt4 key购买 nike

您好,我需要调用函数中的变量,该函数在另一个函数中声明和定义它。有人能帮帮我吗。

import sys
import os
import tkMessageBox
from Tkinter import *
from tkCommonDialog import Dialog
import shutil
import tkFileDialog
import win32com.client

win = Tk()
win.title("Copying the Directory to specified location")
win.geometry("600x600+200+50")
win.resizable()
global src
global des
class Copy():
#global src
#global des
def __init__(self):
def srce():
src = tkFileDialog.askdirectory(title = "The source folder is ")
textboxsrc.delete(0,END)
textboxsrc.insert(0,src)
print src
return src

textboxsrc = Entry(win, width="70")
textboxsrc.insert(0,'Enter master file name')
textboxsrc.pack()
textboxsrc.place(relx=0.40, rely=0.06, anchor=CENTER)
bu = Button(text = "Source",font = "Verdana 12 italic bold",bg = "Purple",fg= "white", command= srce)
bu.pack(fill =X, expand=YES)
bu.place(relx=0.85, rely=0.06, anchor=CENTER)

def dest():
des = tkFileDialog.askdirectory(title = "TheDestination folder is ")
textboxdes.delete(0,END)
textboxdes.insert(0,des)
print des
return des

textboxdes = Entry(win, width="70")
textboxdes.insert(0,'Enter master file name')
textboxdes.pack()
textboxdes.place(relx=0.40, rely=0.13, anchor=CENTER)
bu1 = Button(text = "Destination",font = "Verdana 12 italic",bg = "Purple",fg= "white", command= dest)
bu1.pack(fill =X, expand=YES)
bu1.place(relx=0.85, rely=0.13, anchor=CENTER)

def start():
#global src
#global des
#abc = os.path.dirname(src)
#dgh = os.path.dirname(des)
try:
shutil.copy(src,des)
except :
tkMessageBox.showwarning("Copying file", "Error while copying\n(%s)" )

bn =Button(text = "Copy",font = "Verdana 12 italic", bg = "Purple",fg= "white",command=start)
bn.pack(fill =X, expand = YES)
bn.place(relx=0.50, rely=0.25, anchor=CENTER)

obj= Copy()
#obj.source(win)
#obj.destination(win)
win.mainloop()

在这里,我在 start() 函数中遇到错误。给我接受 src 和 des 变量的问题。

最佳答案

我更新了您的代码(并在阅读您的反馈后进行了必要的更正)以使用类变量/属性并修复了您的错误消息,请查看。请注意,使用类变量意味着您只能将此类作为单例使用,一次只能使用这些类中的一个,因为每个实例都会使用相同的 Copy.src、Copy.des、 Copy.textboxsrc 和 Copy.textboxdsc 变量。

import sys
import os
import tkMessageBox
from Tkinter import *
from tkCommonDialog import Dialog
import shutil
import tkFileDialog
import win32com.client

win = Tk()
win.title("Copying the Directory to specified location")
win.geometry("600x600+200+50")
win.resizable()

# force "new" Python class by inheriting from "object"
class Copy(object):

# use class attributes for shared variables
src = None
des = None
textboxsrc = None
textboxdes = None

def srce():
# access the class attributes via "Copy." syntax
Copy.src = tkFileDialog.askdirectory(title = "The source folder is ")
Copy.textboxsrc.delete(0,END)
Copy.textboxsrc.insert(0,Copy.src)
print Copy.src
return Copy.src

textboxsrc = Entry(win, width="70")
textboxsrc.insert(0,'Enter master file name')
textboxsrc.pack()
textboxsrc.place(relx=0.40, rely=0.06, anchor=CENTER)
bu = Button(text = "Source",font = "Verdana 12 italic bold",bg = "Purple",fg= "white", command= srce)
bu.pack(fill =X, expand=YES)
bu.place(relx=0.85, rely=0.06, anchor=CENTER)

def dest():
# access the class attributes via "Copy." syntax
Copy.des = tkFileDialog.askdirectory(title = "TheDestination folder is ")
Copy.textboxdes.delete(0,END)
Copy.textboxdes.insert(0,Copy.des)
print Copy.des
return Copy.des

textboxdes = Entry(win, width="70")
textboxdes.insert(0,'Enter master file name')
textboxdes.pack()
textboxdes.place(relx=0.40, rely=0.13, anchor=CENTER)
bu1 = Button(text = "Destination",font = "Verdana 12 italic",bg = "Purple",fg= "white", command= dest)
bu1.pack(fill =X, expand=YES)
bu1.place(relx=0.85, rely=0.13, anchor=CENTER)

def start():
# access the class attributes via "Copy." syntax
print "copy src(%s) to des(%s)" % (Copy.src,Copy.des)
try:
shutil.copy(Copy.src,Copy.des)
except:
tkMessageBox.showwarning("Copying file", "Error while copying\n(%s) to (%s)\n%s\n%s"
% (Copy.src,Copy.des, sys.exc_info()[0], sys.exc_info()[1]) )


bn =Button(text = "Copy",font = "Verdana 12 italic", bg = "Purple",fg= "white",command=start)
bn.pack(fill =X, expand = YES)
bn.place(relx=0.50, rely=0.25, anchor=CENTER)

obj= Copy()
win.mainloop()

关于python - python中变量的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8585356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com