简体   繁体   English

如何在 var 上存储 Tkinter.Button 的值

[英]How to store a value from Tkinter.Button on a var

I'm novice with python. So i'm trying to make a GUI, when i click the button will open a directory selector(see on code 1),and i need storage the path from selection that i expected receive on "command" in a var, so i can use the directory path for another things.我是 python 的新手。所以我正在尝试制作一个 GUI,当我单击该按钮时将打开一个目录选择器(参见代码 1),我需要存储我希望在“命令”上收到的选择路径在 var 中,因此我可以将目录路径用于其他用途。

Code 1 css.py代码 1 css.py

...
def selectFolder():
    path = customtkinter.filedialog.askdirectory()
    return path

Code 2 css.py代码 2 css.py

def button(root,txt,event = any):
    button = customtkinter.CTkButton(master = root,text=txt,command = event)
    return button;
...

Main code main.py主要代码main.py

root = css.root()
frame = css.frame(root)
frame.pack(pady=20, padx=60, fill='both',expand=True)
label = css.label(frame,'Tecverde - Engenharia S.A','Roboto',10)
label.pack(pady=12, padx=10)
path = css.button(frame,"Selecione a pasta",css.selectFolder)
path.pack()
root.mainloop()
  1. You are using custontkinter, so all your widgets are 'CTk...'您正在使用 custontkinter,所以您所有的小部件都是“CTk ...”
  2. Your button widgets can contain 'commands', my suggestion is, inside the function passed to the 'ask' command you can use tkinter's filedialog module, and use the output as needed.您的按钮小部件可以包含“命令”,我的建议是,在传递给“询问”命令的 function 中,您可以使用 tkinter 的文件对话框模块,并根据需要使用 output。

This is an example of using Stringvar, a tkinter dynamic variable, whenever you use the command set('') in it you will modify its value for the entire code, and to get the value you can use.get () anywhere in the code.这是一个使用Stringvar的例子,一个tkinter的动态变量,每当你在其中使用set('')命令时,整个代码都会修改它的值,而要获取这个值你可以在任何地方使用get()代码。 Note that the debug label text changes after you dynamically select a directory.请注意,调试 label 文本会在您动态 select 目录后更改。

from tkinter import filedialog

import customtkinter as css

root = css.CTk()
frame = css.CTkFrame(root)
frame.pack(pady=20, padx=60, fill='both', expand=True)
label = css.CTkLabel(frame, text='Tecverde - Engenharia S.A', font=('Roboto', 10))
label.pack(pady=12, padx=10)


def ask():
    dir = filedialog.askdirectory()
    path_var.set(dir)


path_var = css.StringVar(value='Empty Path')

path_button = css.CTkButton(frame, text="Selecione a pasta",
                            command=ask)
path_button.pack()

debug_label = css.CTkLabel(root, textvariable=path_var)
debug_label.pack()

root.mainloop()

In the second example, now using classes, self.path_var can be used anywhere in your code, even with a Stringvar as in the previous example, but now it has more options.在第二个示例中,现在使用类,self.path_var 可以在您的代码中的任何地方使用,甚至可以像上一个示例中那样使用 Stringvar,但现在它有更多选项。

from tkinter import filedialog

import customtkinter as css


class App(css.CTk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        frame = css.CTkFrame(self)
        frame.pack(pady=20, padx=60, fill='both', expand=True)

        label = css.CTkLabel(frame, text='Tecverde - Engenharia S.A', font=('Roboto', 10))
        label.pack(pady=12, padx=10)

        self.path_var = 'Empty path'

        path_button = css.CTkButton(frame, text="Selecione a pasta",
                                    command=self.ask)
        path_button.pack()

        self.debug_label = css.CTkLabel(self, text=self.path_var)
        self.debug_label.pack()

    def ask(self):
        dir = filedialog.askdirectory()
        self.path_var = dir
        self.debug_label.configure(text=dir)


if __name__ == '__main__':
    app = App()
    app.mainloop()

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

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