![](/img/trans.png)
[英]how can i know the path of a file created by os.makedirs() and store it in a variable for later use?
[英]How to store file path in variable which can use in other function?
我想使用gtk在python中做比較和合並工具。 在這個程序中,我想將文件路徑存儲在全局變量中,以便在合並功能中使用。 用於存儲第一文件路徑的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()
正如注釋中已經描述的那樣,您應該使用屬性而不是全局變量。 因此,您使用:
self.text1 = ""
self.text2 = ""
代替:
global text1
global text2
然后,您可以使用self.text1
在類的每個方法中訪問text1和text2屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.