繁体   English   中英

如何将文件路径存储在可以在其他函数中使用的变量中?

[英]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.

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