【问题标题】:Is there a way to save the result of a button's command of tkinter with the def written in a module?有没有办法用模块中编写的 def 来保存 tkinter 按钮命令的结果?
【发布时间】:2020-12-15 13:17:34
【问题描述】:

我在 Python 中几乎是新手,所以如果我的代码没有以正确的“pythonic 方式”编写,请原谅我...... 我想从列表框中保存用户的选择。该函数在 BES_library 模块中定义(我需要函数保留在模块中而不是脚本中!):

##BES_library
def quit(root, listbox, choice):
    #get the user's choice
    choice = listbox.get(listbox.curselection())
    print(choice)

    #quit the ListBox
    root.destroy()
    root.quit()   

    return choice

这是我的脚本:

import tkinter           as tk
import BES_library       as BES_lib

if __name__ == '__main__':
    SheetsNames = ['Foglio1', 'Foglio2', 'Foglio3']
    SheetName = 'lalala'
    
    root = tk.Tk()
    root.title("Select the worksheet to be opened")
    listbox = tk.Listbox(root, selectmode= 'single')
    for item in SheetsNames:
       listbox.insert("end", item)
       listbox.pack()
    button = tk.Button(root, text='Select', command= lambda: BES_lib.quit(root, listbox, SheetName) ) 
    button.pack()
    root.geometry("450x300+120+120")
    root.mainloop()

    print(SheetName)

#now I want to work with the SheetName chosen by the user

用户应该从列表框中选择一个选项,然后单击“选择”按钮。单击按钮后,我想将他的选择作为字符串归因于变量并退出列表框。

问题是,我如何使用用户输入的内容?按钮不保存函数的返回值。

我也尝试了这段代码,但似乎没有任何改变:

import tkinter           as tk
import BES_library       as BES_lib

if __name__ == '__main__':
    SheetsNames = ['Foglio1', 'Foglio2', 'Foglio3']
        
    root = tk.Tk()
    root.title("Select the worksheet to be opened")

    SheetName = tk.StringVar()

    listbox = tk.Listbox(root, selectmode= 'single')
    for item in SheetsNames:
       listbox.insert("end", item)
       listbox.pack()
    button = tk.Button(root, text='Select', command= lambda: BES_lib.quit(root, listbox, SheetName) ) 
    button.pack()
    root.geometry("450x300+120+120")
    root.mainloop()

    print(SheetName.get())

 #now I want to work with the SheetName chosen by the user

【问题讨论】:

  • 你知道 listbox.get('ANCHOR') 吗?
  • 要销毁小部件,您可以使用 pack_forget()place_forget(),具体取决于您使用的管理器

标签: python variables tkinter return


【解决方案1】:

这是一种完全不需要使用quit 方法的方法。无论如何,您的退出方法没有意义。它声称删除了listbox,但它破坏了应用程序的整个root。你也有一个误解~你在调用root.mainloop之后试图打印一些东西。在root.mainloop 之后您所做的任何事情都不会受到打击。 root.mainloop 必须是程序的最后一行。

使用我的方法,将listboxbutton 放在一个框架中。每次用户单击listbox 时,它都会存储选择。调用sheetframe.pack() 显示选择框并单击select 解包/隐藏它。通过这种方式,select 没有选择任何内容。在单击 listbox 项目时,选择已被记录。

import tkinter as tk

#init root
root = tk.Tk()
root.title("Select the worksheet to be opened")
root.geometry("450x300+120+120")

#init vars
sheetlist  = ['Foglio1', 'Foglio2', 'Foglio3']
sheetname  = 'lalala'

#container for listbox and button
sheetframe = tk.Frame(root)

listbox    = tk.Listbox(sheetframe, selectmode='single', listvariable=tk.StringVar(value=sheetlist))
listbox.pack()

#unpack/hide sheetframe on click
tk.Button(sheetframe, text='Select', command=sheetframe.pack_forget).pack()

#store a listbox selection a soon as it is clicked    
def selection(event):
    global sheetname
    sheetname = listbox.get('anchor')
    print(f'{sheetname} selected')

#bind on ButtonRelease so anchor is definitely set
listbox.bind('<ButtonRelease-1>', selection)

#show sheetframe
sheetframe.pack()

root.mainloop()

【讨论】:

  • 感谢您的回答!特别是我学会了如何在不使用 for 循环的情况下创建一个列表框!但不幸的是,您没有回答我的问题……我的功能(“退出”,甚至您的“选择”)必须在模块中定义,而不是在脚本中(实际上对于其他脚本必须是通用的) .我的问题是,在脚本中我无法使用所选字符串获取变量:实际上我不需要打印它,但我想保存它以供以后使用...
  • @user14171495 ~ 当然这不是强制性的。这样做甚至没有意义,但如果你必须〜把它粘在一个模块中并从那里使用它。我回答的问题比你问的多。我对您问题的模块部分的回答是“不要将其粘贴在模块中。将其与使用它的小部件一起保存。”
  • 是的,绝对!你回答的问题比我问的要多(为此我非常感谢你)!当然,一般来说,在模块中定义一个函数不是强制性的......但在这种情况下,我收到的指令是这个......不要问我为什么......所以我试图坚持你在一个模块中的功能,但它仍然不能解决我的问题。所以我现在的问题是:有没有办法做到这一点?你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 2013-06-30
相关资源
最近更新 更多