使用Tkinter将值传递给python脚本

时间:2015-04-19 18:38:43

标签: python tkinter

所以我编写了一个python脚本,它使用PIL库来格式化传递给它的png。我想让脚本更加用户友好,环顾四周之后,我看到了Tkinter库看起来很完美。基本上,脚本有五个变量需要传递给它才能运行。我目前正在使用raw_input()函数向用户询问以下变量:

path_to_png
title
subtitle
source
sample_size

收到脚本后运行并导出格式化的png。我已经使用Tkinter构建了这些基本输入,如下图所示,但我不知道如何将输入的文本值和文件路径从选择png按钮传递到各自的变量。 / p>

enter image description here

from Tkinter import *
from tkFileDialog import askopenfilename
from tkMessageBox import *

app = Tk()
app.title("Picture Formatting")
app.geometry('500x350+200+200')

# 
def callback():
    chart_path = askopenfilename()
    return

def title_data():
    title_data = chart_title
    return

errmsg = 'Error!'
browse_botton = Button(app, text="Choose png", width=15, command=callback)
browse_botton.pack(side='top', padx=15, pady=15)

# Get chart data
chart_title = StringVar()
title = Entry(app, textvariable = chart_title)
title.pack(padx=15, pady=15)

chart_subtitle = StringVar()
subtitle = Entry(app, textvariable = chart_subtitle)
subtitle.pack(padx=15, pady=15)

chart_source = StringVar()
source = Entry(app, textvariable = chart_source)
source.pack(padx=15, pady=15)

chart_sample_size = IntVar()
sample_size = Entry(app, textvariable = chart_sample_size)
sample_size.pack(padx=15, pady=15)

submit_button = Button(app, text="Submit", width=15)
submit_button.pack(side='bottom', padx=15, pady=15)

app.mainloop()

2 个答案:

答案 0 :(得分:2)

我已经尝试过你的代码了,我添加了一些代码:

from Tkinter import *
from tkFileDialog import askopenfilename
from tkMessageBox import *

app = Tk()
app.title("Picture Formatting")
app.geometry('500x350+200+200')

# 
def callback():
    global chart_path
    chart_path = askopenfilename()
    return

def title_data():
    title_data = chart_title
    return

def calculate():
    chart_title = title.get()
    chart_subtitle = subtitle.get()
    chart_source = source.get()
    chart_sample_size = sample_size.get()

    print "chart_path : ", chart_path
    print "chart_title : ", chart_title
    print "chart_subtitle : ", chart_subtitle
    print "chart_source : ", chart_source
    print "chart_sample_size : ", chart_sample_size

    #Call your functions here

    return

errmsg = 'Error!'

# Get chart data

chart_path = ''
browse_botton = Button(app, text="Choose png", width=15, command=callback)
browse_botton.pack(side='top', padx=15, pady=15)

chart_title = StringVar()
title = Entry(app, textvariable = chart_title)
title.pack(padx=15, pady=15)

chart_subtitle = StringVar()
subtitle = Entry(app, textvariable = chart_subtitle)
subtitle.pack(padx=15, pady=15)

chart_source = StringVar()
source = Entry(app, textvariable = chart_source)
source.pack(padx=15, pady=15)

chart_sample_size = IntVar()
sample_size = Entry(app, textvariable = chart_sample_size)
sample_size.pack(padx=15, pady=15)

submit_button = Button(app, text="Submit", width=15, command=calculate)
submit_button.pack(side='bottom', padx=15, pady=15)

app.mainloop()

我认为您想问的问题是如何获取条目小部件中的文本值并从askopenfilename()函数获取路径文本。您可以使用方法Entry.get()来获取特定条目窗口小部件中的文本值。

您可以使用str = askopenfilename()获取路径的文本值。但是因为这行代码是在函数中编写的,所以你需要声明它是一个全局变量或创建一个包含它们的类,或者解释器会认为该变量是一个局部变量而且它不会被传递给函数我添加了calculate()

由于您没有使用类来包含变量,因此我也将变量用作全局变量。这不是一个好的设计。您可以考虑改为创建一个类。

答案 1 :(得分:1)

为了从条目小部件接收值,您可能希望使用get()函数。 get()函数将返回条目小部件中的任何内容。例如,在您的情况下:response = sample_size.get()将变量响应设置为0.有关条目小部件的更多文档,请参阅此page

希望这会有所帮助。