简体   繁体   中英

How to store file path in variable which can use in other function?

I Want to make Compare and merge tool in python using gtk. In this program,I want to store file path in global variable for using in merge function. text1 for storing first file path and text2 for storing second file path. Is There is any other way to using both file path in merge function.?

#!/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()

As it is already described in a comment, you should use an attribute instead of a global variable. So you use:

self.text1 = ""
self.text2 = ""

instead of:

global text1
global text2

Then you can access the text1 and text2 attribute in every method of the class with self.text1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM