在函数内部更新一个全局的Python变量。

huangapple go评论117阅读模式
英文:

Update a global python variable inside a function

问题

I have a function inside a method of a class like this:

  1. from tkinter import Frame, Tk, BOTH, Text, Menu, END
  2. from tkinter import filedialog as tkFileDialog
  3. class Example(Frame):
  4. def __init__(self, parent):
  5. Frame.__init__(self, parent)
  6. self.parent = parent
  7. self.initUI()
  8. self.path = None
  9. print(self.path)
  10. def initUI(self):
  11. self.parent.title("File dialog")
  12. self.pack(fill=BOTH, expand=1)
  13. menubar = Menu(self.parent)
  14. self.parent.config(menu=menubar)
  15. fileMenu = Menu(menubar)
  16. fileMenu.add_command(label="Open_image", command=self.onOpen1)
  17. menubar.add_cascade(label="File", menu=fileMenu)
  18. def onOpen1(self):
  19. ftypes = [('jpg files','.jpg'),('png files','.png'),('all files','.*')]
  20. dlg = tkFileDialog.Open(self, filetypes = ftypes)
  21. path = dlg.show()
  22. if path != '':
  23. self.path = path
  24. print(self.path)
  25. return self.path
  26. def main():
  27. root = Tk()
  28. ex = Example(root)
  29. root.geometry("300x250+300+300")
  30. root.mainloop()
  31. if __name__ == '__main__':
  32. main()

When I run the code, the self.path variable inside the function changes. But the self.path global variable outside the function does not change.

I would like self.path to be updated outside of the function. In fact, I want to update the self.path variable with the onOpen1 function. This function opens a window which allows you to select an image. Then, I want to recover the path of this image to update self.path.

英文:

I have a function inside a method of a class like this:

  1. from tkinter import Frame, Tk, BOTH, Text, Menu, END
  2. from tkinter import filedialog as tkFileDialog
  3. class Example(Frame):
  4. def __init__(self, parent):
  5. Frame.__init__(self, parent)
  6. self.parent = parent
  7. self.initUI()
  8. self.path = None
  9. print(self.path)
  10. def initUI(self):
  11. self.parent.title("File dialog")
  12. self.pack(fill=BOTH, expand=1)
  13. menubar = Menu(self.parent)
  14. self.parent.config(menu=menubar)
  15. fileMenu = Menu(menubar)
  16. fileMenu.add_command(label="Open_image", command=self.onOpen1)
  17. menubar.add_cascade(label="File", menu=fileMenu)
  18. def onOpen1(self):
  19. ftypes = [('jpg files','.jpg'),('png files','.png'),('all files','.*')]
  20. dlg = tkFileDialog.Open(self, filetypes = ftypes)
  21. path = dlg.show()
  22. if path != '':
  23. self.path = path
  24. print(self.path)
  25. return self.path
  26. def main():
  27. root = Tk()
  28. ex = Example(root)
  29. root.geometry("300x250+300+300")
  30. root.mainloop()
  31. if __name__ == '__main__':
  32. main()

When I run the code, the self.path variable inside the function changes. But the self.path global variable outside the function does not change.

I would like self.path to be updated outside of the function. In fact, I want to update the self.path variable with the onOpen1 function. This function opens a window which allows you to select an image. Then, I want to recover the path of this image to update self.path.

答案1

得分: 0

以下是翻译好的代码部分:

  1. from tkinter import Frame, Tk, BOTH, Text, Menu, END, Button
  2. from tkinter import filedialog as tkFileDialog
  3. class Example(Frame):
  4. def __init__(self, parent):
  5. Frame.__init__(self, parent)
  6. self.parent = parent
  7. self.initUI()
  8. self.path = None
  9. print(self.path)
  10. def initUI(self):
  11. self.parent.title("File dialog")
  12. self.pack(fill=BOTH, expand=1)
  13. menubar = Menu(self.parent)
  14. self.parent.config(menu=menubar)
  15. fileMenu = Menu(menubar)
  16. fileMenu.add_command(label="Open_image", command=self.onOpen1)
  17. menubar.add_cascade(label="File", menu=fileMenu)
  18. button = Button(self.parent, text='test', command=self.test)
  19. button.pack()
  20. def test(self):
  21. print(self.path)
  22. def onOpen1(self):
  23. ftypes = [('jpg files','.jpg'),('png files','.png'),('all files','.*')]
  24. dlg = tkFileDialog.Open(self, filetypes = ftypes)
  25. path = dlg.show()
  26. if path != '':
  27. self.path = path
  28. print(self.path)
  29. return self.path
  30. def main():
  31. root = Tk()
  32. ex = Example(root)
  33. root.geometry("300x250+300+300")
  34. root.mainloop()
  35. if __name__ == '__main__':
  36. main()

请注意,这只是代码的一部分。如果您需要翻译更多部分,请提供具体内容。

英文:

The button is just a way to help visualize what is happening. Here is the full code.

  1. from tkinter import Frame, Tk, BOTH, Text, Menu, END, Button
  2. from tkinter import filedialog as tkFileDialog
  3. class Example(Frame):
  4. def __init__(self, parent):
  5. Frame.__init__(self, parent)
  6. self.parent = parent
  7. self.initUI()
  8. self.path = None
  9. print(self.path)
  10. def initUI(self):
  11. self.parent.title("File dialog")
  12. self.pack(fill=BOTH, expand=1)
  13. menubar = Menu(self.parent)
  14. self.parent.config(menu=menubar)
  15. fileMenu = Menu(menubar)
  16. fileMenu.add_command(label="Open_image", command=self.onOpen1)
  17. menubar.add_cascade(label="File", menu=fileMenu)
  18. button = Button(self.parent, text='test', command=self.test)
  19. button.pack()
  20. def test(self):
  21. print(self.path)
  22. def onOpen1(self):
  23. ftypes = [('jpg files','.jpg'),('png files','.png'),('all files','.*')]
  24. dlg = tkFileDialog.Open(self, filetypes = ftypes)
  25. path = dlg.show()
  26. if path != '':
  27. self.path = path
  28. print(self.path)
  29. return self.path
  30. def main():
  31. root = Tk()
  32. ex = Example(root)
  33. root.geometry("300x250+300+300")
  34. root.mainloop()
  35. if __name__ == '__main__':
  36. main()

Run it and watch your console where the print will happen.
Hit the button. Notice how self.path is None.

Now select an image with your file dialog box.

Hit the button again. Notice how self.path changed to reflect what the path to the image is?

You stated that you want to update self.path to reflect what you selected. Your code does in fact do that. The point of this visualization is to show you that the question you asked may not be the solution to the problem you're having.

Here is an example of what I think you want.

  1. from tkinter import Frame, Tk, BOTH, Text, Menu, END, Button, Label
  2. from tkinter import filedialog as tkFileDialog
  3. import cv2
  4. from PIL import ImageTk, Image
  5. class Example(Frame):
  6. def __init__(self, parent):
  7. Frame.__init__(self, parent)
  8. self.parent = parent
  9. self.initUI()
  10. self.path = None
  11. print(self.path)
  12. def initUI(self):
  13. self.parent.title("File dialog")
  14. self.pack(fill=BOTH, expand=1)
  15. menubar = Menu(self.parent)
  16. self.parent.config(menu=menubar)
  17. fileMenu = Menu(menubar)
  18. fileMenu.add_command(label="Open_image", command=self.onOpen1)
  19. menubar.add_cascade(label="File", menu=fileMenu)
  20. def test(self):
  21. print(self.path)
  22. def onOpen1(self):
  23. ftypes = [('jpg files','.jpg'),('png files','.png'),('all files','.*')]
  24. dlg = tkFileDialog.Open(self, filetypes = ftypes)
  25. path = dlg.show()
  26. if path != '':
  27. self.path = path
  28. self.add_image()
  29. def add_image(self):
  30. img = cv2.imread(self.path)
  31. img = cv2.resize(img,(224, 224), interpolation = cv2.INTER_AREA)
  32. image1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  33. image1 = Image.fromarray(image1)
  34. image1 = ImageTk.PhotoImage(image1)
  35. panelA = Label(self, image=image1)
  36. panelA.image = image1
  37. panelA.grid(row=0, column=0, sticky='w')
  38. def main():
  39. root = Tk()
  40. ex = Example(root)
  41. root.geometry("300x250+300+300")
  42. root.mainloop()
  43. if __name__ == '__main__':
  44. main()

Here's some sample code for 2 frames.

  1. from tkinter import Frame, Tk, BOTH, Text, Menu, END, Button, Label
  2. from tkinter import filedialog as tkFileDialog
  3. import cv2
  4. from PIL import ImageTk, Image
  5. class Example(Frame):
  6. def __init__(self, parent):
  7. Frame.__init__(self, parent)
  8. self.parent = parent
  9. self.initUI()
  10. self.current_window = 0
  11. self.path = None
  12. self.panelA = None
  13. self.panelB = None
  14. print(self.path)
  15. def initUI(self):
  16. self.parent.title("File dialog")
  17. #self.pack(fill=BOTH, expand=1)
  18. menubar = Menu(self.parent)
  19. self.parent.config(menu=menubar)
  20. fileMenu = Menu(menubar)
  21. fileMenu.add_command(label="Open Image 1", command=lambda: self.onOpen1(1))
  22. fileMenu.add_command(label="Open Image 2", command=lambda: self.onOpen1(2))
  23. menubar.add_cascade(label="File", menu=fileMenu)
  24. self.button_1 = Button(self.parent, text='Window 1', command=lambda: self.switch_window(1))
  25. self.button_2 = Button(self.parent, text='Window 2', command=lambda: self.switch_window(2))
  26. self.back_button = Button(self.parent, text='Back', command=lambda: self.switch_window(0))
  27. self.button_1.grid(sticky='w')
  28. self.button_2.grid(sticky='w')
  29. def switch_window(self, window):
  30. self.current_window = window
  31. if window == 0:
  32. self.back_button.grid_forget()
  33. self.button_1.grid(sticky='w')
  34. self.button_2.grid(sticky='w')
  35. else:
  36. self.button_1.grid_forget()
  37. self.button_2.grid_forget()
  38. self.back_button.grid(row=1, sticky='w')
  39. self.repopulate_windows()
  40. def onOpen1(self, image_selection):
  41. ftypes = [('jpg files','.jpg'),('png files','.png'),('all files','.*')]
  42. dlg = tkFileDialog.Open(self, filetypes = ftypes)
  43. path = dlg.show()
  44. if path != '':
  45. self.path = path
  46. self.add_image(image_selection)
  47. def add_image(self, image_selection):
  48. img = cv2.imread(self.path)
  49. img = cv2.resize(img,(224, 224), interpolation = cv2.INTER_AREA)
  50. image1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  51. image1 = Image.fromarray(image1)
  52. image1 = ImageTk.PhotoImage(image1)
  53. if image_selection == 1:
  54. self.panelA = Label(self.parent, image=image1)
  55. self.panelA.image = image1
  56. else:
  57. self.panelB = Label(self.parent, image=image1)
  58. self.panelB.image = image1
  59. if self.current_window != 0:
  60. self.repopulate_windows()
  61. def repopulate_windows(self):
  62. if self.current_window == 1:
  63. if self.panelB is not None:
  64. self.panelB.grid_forget()
  65. if self.panelA is not None:
  66. self.panelA.grid(row=0, column=0, sticky='w')
  67. elif self.current_window == 2:
  68. if self.panelA is not None:
  69. self.panelA.grid_forget()
  70. if self.panelB is not None:
  71. self.panelB.grid(row=0, column=0, sticky='w')
  72. else:
  73. if self.panelA is not None:
  74. self.panelA.grid_forget()
  75. if self.panelB is not None:
  76. self.panelB.grid_forget()
  77. def main():
  78. root = Tk()
  79. ex = Example(root)
  80. root.geometry("300x250+300+300")
  81. root.mainloop()
  82. if __name__ == '__main__':
  83. main()

huangapple
  • 本文由 发表于 2020年1月3日 22:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定