0

So I've written a python script that uses the PIL library to format a png passed to it. I want to make the script more user friendly and after looking around I saw the Tkinter library which seemed perfect. Basically the script has five variables that I need to pass to it for it to run. I'm currently using the raw_input() function to asking the user for the following variables:

path_to_png
title
subtitle
source
sample_size

Once received the script runs and exports the formatted png. I've used Tkinter to build those basic inputs as you can see from the picture below but I don't know how to pass the inputed text values and the file path from the choose png button to their respective variables.

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()
3
  • If I understand the question correctly you want to set the text in the entry widget to some string collected in a different function right? Or do you want to get the values from the entry widgets? Commented Apr 19, 2015 at 18:56
  • I believe I want to get the values from the entry widget and pass those to the script when submit is clicked. Commented Apr 19, 2015 at 19:02
  • You don't need to create a IntVar or StringVar for each entry. That's just adding extra overhead, since you can get the value directly from the widget. Commented Apr 19, 2015 at 19:58

2 Answers 2

2

I have tried your code, and I added some lines:

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

I think the problem you want to ask is how to get the text values in the entry widgets and get the path text from the askopenfilename() function. You can use the method Entry.get() to get the text value in certain entry widget.

And you can just use str = askopenfilename() to get the path's text value. But because this line of code is written in a function, you need to declare that it is a global variable or create a class to contain them, or the interpreter will consider that variable is an local variable and it will not be passed to the function calculate() which I added.

Since you didn't use a class to contain the variables, I also use the variables as global variables. It is not a good design. You can consider to create a class instead.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok I will try this. How do I design the submit button so it passes the values and runs the script each time its pressed and clears the information in the boxes and path
I have added an function named "calculate" as the callback function of the submit button and mentioned you where you can call the functions you wrote before. You can try this code. If you want to clean up the boxes, you can use the method "Entry.delete(0, 'end')" to clear the entry widgets. And if you want to clear the path value, you can assign an empty string to the chart_path variable after finishing your calculating. Notice that you need to declare that the chart_path variable is a global variable before you change its value in a function.
This link may help you to design this program: link
hmmm title.delete(0, 'end') doesn't seem to remove the information when submit is clicked
1

In order to receive the value from an entry widget, you would want to use the get() function. The get() function will return whatever is in the entry widget. For instance in your case: response = sample_size.get() will set the variable response to 0. See this page for more documentation on the entry widget.

Hope this helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.