简体   繁体   English

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

[英]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. 我想使用gtk在python中做比较和合并工具。 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. 用于存储第一文件路径的text1和用于存储第二文件路径的text2。 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 . 然后,您可以使用self.text1在类的每个方法中访问text1和text2属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何知道 os.makedirs() 创建的文件的路径并将其存储在变量中供以后使用? - how can i know the path of a file created by os.makedirs() and store it in a variable for later use? 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 如何在文件路径中使用变量 - how to use variable in file path 如何在其他函数中使用返回的变量。 Python - How can I use returned variable in other function. Python 如何确定要在python中定义的函数使用哪个变量 - How can I determine which variable to use for a defined function in python 如何在函数中使用 LOCAL VARIABLE,就像其他函数的 GLOBAL VARIABLE 变量 - How can I use LOCAL VARIABLE in function, like a GLOBAL VARIABLE variable for other function Django:如何使用 other.py 文件中定义的变量? - Django: How can I use variable defined in the other .py file? Django:如何将在随机函数中生成的变量存储在数据库中? - Django: How can I store in database a variable which I generate in a random function? 使用 Tkinter 获取其他 function 使用的文件路径 - Getting file path for use by other function using Tkinter 浏览文件并将路径存储在 function - Browse for a file and store the path in a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM