2

I would like to have a basic GUI with two text box inputs: one for each of the arguments in my function, convert_databases, but I'm not sure how to pass those arguments (I've seen some examples using lambda, but I wasn't able to implement them correctly).

Here my attempt so far, which mostly came from Tkinter's native tutorial:

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

Thanks!

julianstanley
  • 1,367
  • 4
  • 13
  • 26

1 Answers1

4

Dont use string variables I was also stuck on this once instead use entry get methods

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

And use official terms like entry not text box

Burhan Asif
  • 228
  • 5
  • 13
  • 1
    i will give you a link of my tkinter projects i hope they will help you https://github.com/Burhanasif59/Python-modules download the tkinter projects zip.They are in python 2.7 but it wont be a problem – Burhan Asif Jul 25 '17 at 17:34
  • It worked beautifully, it just took me a few minutes to get it working on my end! Thanks much!! – julianstanley Jul 25 '17 at 17:45
  • not a problem please also check my tkinter repository it will help you.I am also learning pyqt and at the same stage as you :) – Burhan Asif Jul 25 '17 at 17:45