如何将文件路径存储在变量中以供其他函数使用?
我想用Python和GTK做一个比较和合并的工具。在这个程序里,我想把文件路径存储在全局变量中,以便在合并函数中使用。text1用来存储第一个文件的路径,text2用来存储第二个文件的路径。有没有其他方法可以在合并函数中使用这两个文件的路径呢?
#!/usr/bin/python
import pygtk,gtk,os
global count
count=0
class pro:
def destroy(self,widget):
print("quit")
#def file_selection(
def file_selection(self,widget,textview,text):
self.filew=gtk.FileSelection("File selection")
self.filew.connect("destroy",self.destroy)
self.filew.ok_button.connect("clicked",self.file_ok_sel,textview,text)
self.filew.cancel_button.connect("clicked",lambda w:self.filew.destroy())
self.filew.set_filename("penguin.png")
self.filew.show()
def file_ok_sel(self,w,textview,text):
print("%s " % self.filew.get_filename())
text=self.filew.get_filename()
print(text)
an="/home/himanshu/a.txt"
self.result=os.system("stat " + text + ">"+an+"")
testbuffer=textview.get_buffer()
infile = open(an, "r")
global count
if(count==0):
text1=text
count=count+1
elif(count==1):
text2=text
if infile:
string = infile.read()
infile.close()
testbuffer.set_text(string)
textview.show()
def callback():
print("pagal")
def merging(self,widget):
print("-------")
print(text1)
print(text2)
print("---------")
def __init__(self):
self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Compare & Merge Tools")
self.window.connect("delete_event",lambda a1,a2:gtk.main_quit())
self.window.set_border_width(10)
self.window.show()
global text1
global text2
text1=text2=""
#main verticalbox
vbox=gtk.VBox(False,0)
self.window.add(vbox)
#--------------------------------------------
#Horizontal box for two files
hbox=gtk.HBox(False,0)
#first vertical box for horizontal
vbox1=gtk.VBox(False,0)
#button1
button1=gtk.Button("Select")
vbox1.pack_start(button1,False,False,0)
button1.show()
#textview1
textview1=gtk.TextView()
textbuffer1=textview1.get_buffer()
text1=button1.connect("clicked",self.file_selection,textview1,text1)
vbox1.pack_start(textview1,False,False,0)
textview1.show()
vbox1.show()
#second vertical box for horizontal
vbox2=gtk.VBox(False,0)
#button2
button2=gtk.Button("select")
vbox2.pack_start(button2,False,0)
button2.show()
#textview2
textview2=gtk.TextView()
textbuffer1=textview2.get_buffer()
text2=button2.connect("clicked",self.file_selection,textview2,text2)
vbox2.pack_start(textview2,False,False,0)
textview2.show()
vbox2.show()
hbox.pack_start(vbox1,False,False,0)
hbox.pack_start(vbox2,False,False,0)
vbox.pack_start(hbox,False,False,0)
hbox.show()
#---------------------------------------------
hbox3=gtk.HBox(False,0)
button_compare=gtk.Button("Compare")
button_merge=gtk.Button("Merge")
hbox3.pack_start(button_compare,False,0)
hbox3.pack_end(button_merge,False,0)
button_merge.connect("clicked",self.merging)
button_compare.show()
button_merge.show()
vbox.pack_start(hbox3,False,False,0)
hbox3.show()
vbox.show()
self.window.show()
def main():
gtk.main()
return
if __name__=="__main__":
pro()
main()
1 个回答
1
正如评论中所说的,你应该使用属性,而不是全局变量。这样你可以使用:
self.text1 = ""
self.text2 = ""
而不是:
global text1
global text2
这样你就可以在这个类的每个方法中通过 self.text1
来访问 text1 和 text2 这两个属性。