使用Tkinter将值传递给python脚本

使用Tkinter将值传递给python脚本,python,tkinter,Python,Tkinter,因此,我编写了一个python脚本,它使用PIL库格式化传递给它的png。我想让脚本更加用户友好,环顾四周后,我看到了Tkinter库,它看起来非常完美。基本上,脚本有五个变量,我需要传递给它才能运行。我目前正在使用raw_input()函数询问用户以下变量: path_to_png title subtitle source sample_size 收到脚本后,运行并导出格式化的png。我使用Tkinter构建了这些基本输入,如下图所示,但我不知道如何将输入的文本值和文件路径从choose

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

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

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()


为了从条目小部件接收值,您需要使用
get()
函数。
get()
函数将返回条目小部件中的任何内容。例如,在您的示例中:
response=sample\u size.get()
将变量response设置为0。有关entry小部件的更多文档,请参见此


希望这有所帮助。

为了从条目小部件接收值,您需要使用
get()
函数。
get()
函数将返回条目小部件中的任何内容。例如,在您的示例中:
response=sample\u size.get()
将变量response设置为0。有关entry小部件的更多文档,请参见此


希望这有所帮助。

为了从条目小部件接收值,您需要使用
get()
函数。
get()
函数将返回条目小部件中的任何内容。例如,在您的示例中:
response=sample\u size.get()
将变量response设置为0。有关entry小部件的更多文档,请参见此


希望这有所帮助。

为了从条目小部件接收值,您需要使用
get()
函数。
get()
函数将返回条目小部件中的任何内容。例如,在您的示例中:
response=sample\u size.get()
将变量response设置为0。有关entry小部件的更多文档,请参见此


希望这有所帮助。

我已经尝试了您的代码,并添加了一些行:

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()
获取路径的文本值。但是因为这行代码是用函数编写的,所以需要声明它是一个全局变量或创建一个包含它们的类,或者解释器会认为该变量是一个局部变量,它不会传递给我添加的函数<代码>计算器()>代码>。
因为您没有使用类来包含变量,所以我也将变量用作全局变量。这不是一个好的设计。你可以考虑创建一个类。

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

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()
获取路径的文本值。但是因为这行代码是用函数编写的,所以需要声明它是一个全局变量或创建一个包含它们的类,或者解释器会认为该变量是一个局部变量,它不会传递给我添加的函数<代码>计算器()>代码>。
因为您没有使用类来包含变量,所以我也将变量用作全局变量。这不是一个好的设计。你可以考虑创建一个类。

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

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()
获取路径的文本值。但是因为这行代码是用函数编写的,所以需要声明它是一个全局变量或创建一个包含它们的类,或者解释器会认为该变量是一个局部变量,它不会传递给我添加的函数<代码>计算器()>代码>。
因为您没有使用类来包含变量,所以我也将变量用作全局变量。这不是一个好的设计。你可以考虑创建一个类。

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

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()
获取路径的文本值。但是因为这行代码是用函数编写的,所以需要声明它是一个全局变量或创建一个包含它们的类,或者解释器会认为该变量是一个局部变量,它不会传递给我添加的函数<代码>计算器()>代码>。
因为您没有使用类来包含变量,所以我也将变量用作全局变量。这不是一个好的设计。你可以考虑创建一个类。

如果我正确地理解了这个问题,你想把条目窗口中的文本设置为用不同函数收集的字符串吗?或者您想从入口小部件获取值吗?我相信我想从入口小部件获取值并传递