【问题标题】:How to set a variable to the result of command function of a tkinter Button?如何将变量设置为 tkinter 按钮的命令函数的结果?
【发布时间】:2017-01-25 05:51:43
【问题描述】:

所以这是我的问题,我无法弄清楚并且似乎无法找到信息来帮助我了解正在发生的事情。所以我将source 设置为等于调用openDirectory 函数的按钮,这实际上只是在tkinter.filedialogaskopendirectory 函数上调用os.path.join()os.path.normalize() 的捷径。

问题是source 始终是一个数字,我不知道为什么它不是我在openDirectory 函数中选择的路径。我也试过将代码直接放在按钮命令中的openDirectory中,它仍然做同样的事情。

重现步骤:

  1. 运行此代码(使用 python 3.5 编写)
  2. 使用源按钮选择路径
  3. 点击右下角的按钮,该按钮应以messagebox 显示路径
  4. 请注意,messagebox 显示的是一个大数字而不是路径。

如何获取存储在源变量中的路径以便随时访问它?

#!/usr/bin/python
import os
from functions import *
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog


class FileMover(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def openDirectory(listfiles, recursive):
        destination = os.path.join(os.path.normpath(filedialog.askdirectory()), "")
        return destination

    def initUI(self):
        self.parent.title("File Mover")
        self.pack()

        recursiveCheck = bool
        previewCheck = bool

        # source button and label. source should equal the path selected in openDirecotry
        source = Button(self, text="Source Directory", command=lambda:openDirectory(recursiveCheck))
        sourceLabel = Label(self, text="Select a Source Directory...")
        sourcemsg = Button(self, text="Source Variable", command=lambda:messagebox.askokcancel(self, source))

        # check box used to tell open directory either true or false to recurse the source dir
        recursiveLabel = Label(self, text="Recursive ")
        recursive = Checkbutton(self, onvalue=True, offvalue=False, variable=recursiveCheck)

        # destination button and label. source should equal the path selected in openDirecotry
        destination = Button(self, text="Target Directory ", command=lambda:openDirectory(False))
        destinationLabel = Label(self, text="Select a Target Directory...")

        # not implemented yet
        previewLabel = Label(self, text="Preview ")
        preview = Checkbutton(self, onvalue=True, offvalue=False, variable=previewCheck)

        source.grid(row=0, column=0, columnspan=2)
        sourceLabel.grid(row=0, column=2)
        recursiveLabel.grid(row=1, column=1)
        recursive.grid(row=1, column=2, sticky=W)
        destination.grid(row=2, column=0, columnspan=2)
        destinationLabel.grid(row=2, column=2)
        previewLabel.grid(row=4, column=6)
        preview.grid(row=4, column=7, sticky=W)
        # just for debugging to show source directory on demand
        sourcemsg.grid(row=5, column=8)

def main():
    root = Tk()
    ex = FileMover(root)
    root.mainloop()

if __name__ == '__main__':
    main()

【问题讨论】:

标签: python python-3.x button tkinter


【解决方案1】:

按钮不是这样工作的。当一个函数作为按钮的回调,通过按钮调用该函数时,无处可返回。没有一种明智的方法可以使这项工作发挥作用。如果它按照您猜测的方式工作,您将失去对按钮的引用!你不想这样。

相反,只需将其保存为实例变量:

def openDirectory(self, recursive):
    self.destination = os.path.join(os.path.normpath(filedialog.askdirectory()), "")

请注意,您的openDirectory 方法有listfiles 指的是实例本身,而其他方法使用传统的self - 我已将其更改为使用self,因此您不必处理listfiles.destination = ....

sourcemsg = Button(self, text="Source Variable", command=lambda:messagebox.askokcancel('Window Title', self.destination))

请注意,我已将messagebox.askokcancel 的参数更改为字符串'Window Title' 和引用self.destination,所以不是窗口的标题是对框架的引用(它应该是窗口的字符串title) 它是一个实际的字符串,而不是消息的文本是对按钮的引用(它应该是消息文本的字符串)它是您保存在 openDirectory 中的字符串。

【讨论】:

  • 你摇滚!我从来没有这样想过,但根据你给出的解释,它是完全有道理的。非常感谢!
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 2012-09-27
  • 2021-01-01
  • 1970-01-01
  • 2016-04-26
相关资源
最近更新 更多