python - Python 中的 Tkinter 初学者 : Functions with Inputs

标签 python user-interface tkinter data-science

我想要一个带有两个文本框输入的基本 GUI:一个用于函数中的每个参数,convert_databases,但我不确定如何传递这些参数(我已经看到了一些使用 lambda 的示例,但我无法正确实现它们)。

这是我迄今为止的尝试,主要来自 Tkinter 的 native 教程:

from tkinter import *
from tkinter import ttk

def convert_databases(input_file, output_format):
    #Function deleted for simplicity

root = Tk()
root.title("Title")

#Formatting
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

#Setting Variables
file = StringVar()
conversion = StringVar()

# Places to enter variables
file_entry = ttk.Entry(mainframe, width=50, textvariable=file)
file_entry.grid(column=2, row=2, sticky=(W, E))
type_entry = ttk.Entry(mainframe, width=50, textvariable=conversion)
type_entry.grid(column=2,row=3, sticky=(W,E))

# Convert Button
ttk.Button(mainframe, text="Convert", command= # Here is where I'm having trouble#)

#Label for the variable 1 input
ttk.Label(mainframe, text="Input file name: ").grid(column=1, row=2, sticky=W)
#Label for the variable 2 input
ttk.Label(mainframe, text="Input file type conversion: ").grid(column=1, row=3, sticky=W)

# This sets the window, I think?
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

# Puts the cursor automatically in the text box
file_entry.focus()

# Runs the thing
root.mainloop()

谢谢!

最佳答案

不要使用字符串变量,我也曾经被困在这个问题上,而是使用条目获取方法

    from tkinter import *
    from tkinter import ttk

    def convert_databases():
            global file
            global convert
            # get the values of entries
            file = file_entry.get()
            convert = type_entry.get()

    root = Tk()
    root.title("Title")

 #Formatting
    mainframe = ttk.Frame(root, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)



   # Places to enter variables
    file_entry = ttk.Entry(mainframe, width=50)
    file_entry.grid(column=2, row=2, sticky=(W, E))
    type_entry = ttk.Entry(mainframe, width=50)
    type_entry.grid(column=2,row=3, sticky=(W,E))

    # Convert Button
    ttk.Button(mainframe, text="Convert", command= convert_databases
    )

    #Label for the variable 1 input
    ttk.Label(mainframe, text="Input file name: ").grid(column=1, row=2, 
    sticky=W)
    #Label for the variable 2 input
    ttk.Label(mainframe, text="Input file type conversion: 
    ").grid(column=1, row=3, sticky=W)

     # This sets the window, I think?
     for child in mainframe.winfo_children(): child.grid_configure(padx=5, 
     pady=5)

     # Puts the cursor automatically in the text box
     file_entry.focus()

     # Runs the thing


     root.mainloop()

并使用官方术语,例如输入而不是文本框

关于python - Python 中的 Tkinter 初学者 : Functions with Inputs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45309881/

相关文章:

python - 验证字符串中是否存在所有字符序列 (Python 2+)

java - Solr 4 Web界面奇怪的查询输出

python - 如何在 Tkinter Python 中删除窗口背景

python - Tkinter tkMessageBox 禁用 Tkinter 键绑定(bind)

python - 使用 __dict__ 列出实例属性

python - 使用 "ComboboxSelected"的一个函数来读取多个组合框

java - 用 Java 实现多个 ActionListener

python - 文本小部件内的 Tkinter 检查按钮和滚动

python - 如何在 Python 中的函数调用之间维护列表和字典?

android - 在 Android 上通过 HTTP 检索和解析 iCal 数据