Get text using entry.get() in tkinter and store into variable - python

Below is code:
import os
from tkinter.filedialog import askopenfilename
from tkinter import *
#~~~~ FUNCTIONS~~~~
def open_file_1():
global file_path_1
filename_1 = askopenfilename()
file_path_1 = os.path.dirname(filename_1) + filename_1
entry_1.delete(0, END)
entry_1.insert(0, file_path_1)
return file_path_1
def open_file_2():
global file_path_2
filename_2 = askopenfilename()
file_path_2 = os.path.dirname(filename_2) + filename_2
entry_3.delete(0, END)
entry_3.insert(0, file_path_2)
return file_path_2
def title_name_1():
global title_1
title_1=str(entry_4.get())
return title_1
def title_name_2():
global title_2
title_2=str(entry_5.get())
return title_2
def process_file(content):
print(file_path_1)
print(file_path_2)
print(title_1)
print(title_2)
#~~~~~~ GUI ~~~~~~~~
root = Tk()
root.title('Test Project')
root.geometry("698x220+350+200")
mf = Frame(root)
mf.pack()
f1 = Frame(mf, width=600, height=250)
f1.pack(fill=X)
f3 = Frame(mf, width=600, height=250)
f3.pack(fill=X)
f4 = Frame(mf, width=600, height=250)
f4.pack(fill=X)
f5 = Frame(mf, width=600, height=250)
f5.pack(fill=X)
f2 = Frame(mf, width=600, height=250)
f2.pack()
file_path_1 = StringVar
file_path_2 = StringVar
title_1 = StringVar
title_2 = StringVar
Label(f1,text="Select File 1").grid(row=0, column=0, sticky='e')
Label(f3,text="Select File 2").grid(row=0, column=0, sticky='e')
Label(f4,text="Title1").grid(row=0, column=0, sticky='e')
Label(f5,text="Title2").grid(row=0, column=0, sticky='e')
entry_1 = Entry(f1, width=50, textvariable=file_path_1)
entry_1.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f1, text="Browse", command=open_file_1).grid(row=0, column=27, sticky='ew', padx=8, pady=4)
entry_3 = Entry(f3, width=50, textvariable=file_path_2)
entry_3.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f3, text="Browse", command=open_file_2).grid(row=0, column=27, sticky='ew', padx=8, pady=4)
entry_4 = Entry(f4, width=50,textvariable=title_1)
entry_4.grid(row=0,column=1,padx=5,pady=2,sticky='we',columnspan=25)
entry_5 = Entry(f5, width=50,textvariable=title_2)
entry_5.grid(row=0,column=1,padx=5,pady=2,sticky='we',columnspan=25)
Button(f2, text="Submit", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=70)
root.mainloop()
I want to get these 4 fields(file_path_1, file_path_2, Title1, Title2) and store them so that can be used for further manipulation.I am using Browse to select files and user will enter text for Title1 and Title2.I am new to this, so didn't have much idea.

You are complicating your life unnecessarily by using StringVar(). StringVar's are mostly useful if you attach observer callbacks to them...
Most of the time for Entry widgets, it is better to get their content with the .get() method just before using it:
def process_file():
# Get Entry box content
filename_1 = entry_1.get()
# Do something with it
print(filename_1)

Related

Tkinter - Unable to figure out how to update windows

First off, I'm new to coding. And I'm following a course on it. But in the meantime I want to test myself and figure things out for myself and "learn on the job" a bit with more hands on coding that I can use right away.
I've written below code to try and figure out to make a main window with 2 buttons. If I press a button, it should change the screen into the second/third screen. But instead if I fire up my exe.
It opens all 3 windows right away in separate windows. And once I press a button it opens another window. But what I would want is that the main window get's "updated" to show only the labels/pictures/buttons etc (which I did not include in the .py yet).
from tkinter import *
def second_window1():
second_window1 = Toplevel(main)
second_window1.title("Second window")
second_window1.geometry("414x896")
Label(second_window1, text ="This is the second window")#.pack()
def third_window1():
third_window1 = Toplevel(main)
third_window1.title("Third window")
third_window1.geometry("414x896")
Label(third_window1, text ="This is the third window")#.pack()
main = Tk()
main.title("Main Screen")
main.geometry('414x896')
main.configure(background = "azure2")
main.resizable(False, False)
Label(main, text = "Label_1", fg = "chartreuse2", bg = "ghostwhite", font = "Helvetica 16 bold").grid(row=1, column=1)
second_window = Tk()
second_window.title("Second Screen")
second_window.geometry('414x896')
second_window.configure(background = "azure2")
second_window.resizable(False, False)
Label(main, text = "Label_2", fg = "chartreuse2", bg = "ghostwhite", font = "Helvetica 16 bold").grid(row=1, column=1)
third_window = Tk()
third_window.title("Third Screen")
third_window.geometry('414x896')
third_window.configure(background = "azure2")
third_window.resizable(False, False)
Label(main, text = "Label_3", fg = "chartreuse2", bg = "ghostwhite", font = "Helvetica 16 bold").grid(row=1, column=1)
btn = Button(main, text ="Second Screen", command = second_window1).grid(row=1, column=1)
btn = Button(main, text ="Third Screen", command = third_window1).grid(row=2, column=1)
mainloop()
enter image description here
You would want to use a ttk.Notebook for that.
See e.g. at TkDocs.
An example that creates a UI using a Notebook is below.
def create_widgets(root, db):
"""Create the UI.
Arguments:
root: root window.
db: a connection to an sqlite3 database or None.
Returns:
A SimpleNamespace of widgets.
"""
# Set the font size.
default_font = nametofont("TkDefaultFont")
default_font.configure(size=12)
root.option_add("*Font", default_font)
# Common bindings
root.bind_all('q', do_q)
# SimpleNamespace to store widgets we need in the callbacks.
w = SimpleNamespace()
# Menu
menubar = tk.Menu(root)
root.config(menu=menubar)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=do_open)
filemenu.add_separator()
filemenu.add_command(label="Close", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
# Notebook
n = ttk.Notebook(root)
w.n = n
n.pack(expand=1, fill='both')
f1 = ttk.Frame(n) # Pagina 1
f1.columnconfigure(1, weight=1)
f1.rowconfigure(1, weight=1)
w.f1 = f1
f2 = ttk.Frame(n) # Pagina 2
w.f2 = f2
f2.columnconfigure(0, weight=1)
f2.rowconfigure(0, weight=1)
f3 = ttk.Frame(n) # Pagina 3
w.f3 = f3
f3.columnconfigure(0, weight=1)
f3.rowconfigure(0, weight=1)
f4 = ttk.Frame(n)
f4.columnconfigure(0, weight=1)
f4.rowconfigure(0, weight=1)
w.f4 = f4
n.add(f2, text='Orders')
n.add(f3, text='Keywords')
n.add(f4, text="Bew. 800")
n.add(f1, text='Find')
# First row of page one
ttk.Label(f1, text='Look for:').grid(row=0, column=0, sticky='w')
ze = ttk.Entry(f1)
ze.grid(row=0, column=1, sticky='ew')
ze.bind('<Return>', do_seek)
w.ze = ze
zb = ttk.Button(
f1,
text='Execute',
command=do_seek
)
if db is None:
ze['state'] = 'disabled'
zb['state'] = 'disabled'
zb.grid(row=0, column=3, sticky='e')
w.zb = zb
# Second row of page 1
cols = ['hours']
tv = ttk.Treeview(f1, columns=cols, selectmode=None)
tv.column("#0", anchor=tk.CENTER)
tv.heading("#0", text="Datum")
tv.column("hours", anchor=tk.CENTER)
tv.heading("hours", text="Hours")
tv.grid(row=1, column=0, columnspan=4, sticky='nesw')
w.tv = tv
vsb = ttk.Scrollbar(f1, orient="vertical", command=tv.yview)
vsb.grid(row=1, column=4, sticky='ns')
tv.configure(yscrollcommand=vsb.set)
# First row of page 2
cols2 = ['order', 'hours']
tv2 = ttk.Treeview(f2, columns=cols2, selectmode=None, show='headings')
for c in cols2:
tv2.heading(c, text=c.capitalize())
tv2.column(c, anchor=tk.CENTER)
tv2.grid(row=0, column=0, sticky='nesw')
w.tv2 = tv2
vsb = ttk.Scrollbar(f2, orient="vertical", command=tv2.yview)
vsb.grid(row=0, column=1, sticky='ns')
tv2.configure(yscrollcommand=vsb.set)
# First row of page 3
cols3 = ['keyword', 'hours']
tv3 = ttk.Treeview(f3, columns=cols3, selectmode=None, show='headings')
for c in cols3:
tv3.heading(c, text=c.capitalize())
tv3.column(c, anchor=tk.CENTER)
tv3.grid(row=0, column=0, sticky='nesw')
w.tv3 = tv3
vsb = ttk.Scrollbar(f3, orient="vertical", command=tv3.yview)
vsb.grid(row=0, column=1, sticky='ns')
tv3.configure(yscrollcommand=vsb.set)
# First for of page 4
cols4 = ['order', 'hours']
tv4 = ttk.Treeview(f4, columns=cols4, selectmode=None, show='headings')
for c in cols4:
tv4.heading(c, text=c.capitalize())
tv4.column(c, anchor=tk.CENTER)
tv4.grid(row=0, column=0, sticky='nesw')
w.tv4 = tv4
vsb = ttk.Scrollbar(f4, orient="vertical", command=tv4.yview)
vsb.grid(row=0, column=1, sticky='ns')
tv4.configure(yscrollcommand=vsb.set)
return w

Why am I getting AttributeError: 'function' object has no attribute 'oracleCreate'?

I can't seem to figure out why I'm getting this error I've done the same thing for the other SQLite class and it works fine. With the Oracle class it has holdups. Mostly I just can't seem to figure out why there's an error in my own class when I know the class is there. Thanks in advance for any help.
createClass.py
from tkinter import *
import MainClass
import SQLiteCreate
import oracleCreate
class createClass():
def __init__(self):
pass
def createpage(self):
def quit():
window.destroy()
def oracleCreate():
quit()
obj = oracleCreate.oracleCreate()
obj.createpage()
def mongoCreate():
pass
def sqliteCreate():
quit()
obj = SQLiteCreate.sqliteCreate()
obj.createpage()
def back():
quit()
obj = MainClass.main_project()
obj.mainpage()
window = Tk()
window.geometry("600x300")
window.title("DATA WORLD")
window.resizable(0, 0)
#header, content, footer
header = Frame(window, bg='dark gray')
header.grid(row=0, sticky='news')
content = Frame(window, bg='black')
content.grid(row=1, sticky='news')
footer = Frame(window, bg='dark gray')
footer.grid(row=2, sticky='news')
window.columnconfigure(0, weight=5)
window.rowconfigure(0, weight=1)
window.rowconfigure(1, weight=8)
window.rowconfigure(2, weight=1)
welcome = Label(header, text="Table Creator", bg='dark gray')
welcome.config(font=("Courier", 30))
welcome.pack()
oracle_but = Button(content, text="Oracle", command=oracleCreate,width=15, height=2)
oracle_but.place(x=75, y=40)
mongo_but = Button(content, text="MongoDB", command=mongoCreate,width=15, height=2)
mongo_but.place(x=375, y=40)
sqlite_but = Button(content, text="SQLite", command=sqliteCreate,width=15, height=2)
sqlite_but.place(x=225, y=40)
back_but = Button(content, text='Back', command=back, width=15, height=2)
back_but.place(x=225, y=100)
#Run Window
window.mainloop()
oracleCreate.py
from tkinter import *
import cx_Oracle
import DBconnect
import createClass
class oracleCreate():
def __init__(self):
pass
def createpage(self):
def quit():
window.destroy()
def createTable():
try:
obj = DBconnect.SQLiteDB()
obj.create(tableName.get(), tableCols.get())
top= Toplevel(window)
top.geometry("500x200")
top.title("Success")
Label(top, text= "Table Created Successfully").place(x=150,y=80)
except cx_Oracle.OperationalError as e:
top= Toplevel(window)
top.geometry("500x200")
top.title("Failed")
Label(top, text= "The table you attempted to create already exists please try another name or use another operation.", font='Arial 8').place(x=0,y=80)
print(e)
def back():
quit()
obj = createClass.createClass()
obj.createpage()
window = Tk()
window.geometry("600x300")
window.title("DATA WORLD")
window.resizable(0, 0)
#Page Format
header = Frame(window, bg='dark gray')
header.grid(row=0, sticky='news')
content = Frame(window, bg='black')
content.grid(row=1, sticky='news')
footer = Frame(window, bg='dark gray')
footer.grid(row=2, sticky='news')
window.columnconfigure(0, weight=5)
window.rowconfigure(0, weight=1)
window.rowconfigure(1, weight=8)
window.rowconfigure(2, weight=1)
welcome = Label(header, text="Data World", bg='dark gray')
welcome.config(font=("Courier", 30))
welcome.pack()
#Entry boxes
tableName = Entry(window, width=60)
tableName.place(x=50, y=125)
tableCols = Entry(window, width=60)
tableCols.place(x=50, y=175)
#Labels
Label(window, text="Table Name: ", fg='white', bg='black').place(x=50, y=100)
Label(window, text="Table Columns and types seperated by comma: ", fg='white', bg='black').place(x=50, y=150)
#Buttons
submit_but = Button(window, text='Submit', command=createTable, width=15)
submit_but.place(x=50, y=200)
back_but = Button(window, text='Back', command=back, width=15)
back_but.place(x=200, y=200)
window.mainloop()
You have a module named oracleCreate, but then inside your createPage() method you define a function named oracleCreate. Within the scope of your createPage() method, this replaces the module you've imported with your function. So when oracleCreate.oracleCreate() executes, Python tries to access an attribute named oracleCreate of your oracleCreate function. This of course doesn't exist, hence the error you get.
You don't have the same problem with SQLite because the module is named SQLiteCreate and you declare a function named sqliteCreate inside createPage().
Rename either your oracleCreate module or your oracleCreate function.

How to set different vairiables for groups of radio buttons in python tkinter?

I'm trying to make a MCQ test page with tkinter in python.
Is there a way to initialize/declare multiple variables as StringVar() inside a loop so that each question has its own group of radio button. That part is in show_test() function.
I currently have this -
from tkinter import *
from tkinter import ttk
import json
class test_page():
def __init__(self, master, file_name):
global answer_json
self.master = master
self.file_name = file_name
answer_json = {}
answer_json['objective'] = []
self.master.geometry("1280x720")
self.master.attributes('-fullscreen', True)
main_frame = Frame(self.master)
main_frame.pack(fill=BOTH, expand=1)
# Create A Canvas
my_canvas = Canvas(main_frame)
my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
# Add A Scrollbar To The Canvas
my_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
# Configure The Canvas
my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))
# Create ANOTHER Frame INSIDE the Canvas
self.test_frame = Frame(my_canvas)
# Add that New frame To a Window In The Canvas
my_canvas.create_window((0,0), window=self.test_frame, anchor="nw")
self.show_test()
def append_answer(self, qno, answer, type_):
answer_json[type_].append({"qno": qno,"answer": answer})
def show_test(self):
with open(self.file_name,'r') as file:
count=k=0
data = json.load(file)
for q in data['objective']:
option = StringVar()
option.set("None")
lbl_qno = Label(self.test_frame, text=q['qno'])
lbl_qno.grid(row=count, column=0, padx=(0,10), pady=15)
lbl_question = Label(self.test_frame, width=30, text=q['question'])
lbl_question.grid(row=count, column=1, padx=(10,30))
lbl_marks = Label(self.test_frame, text=f"[{q['marks']}]")
lbl_marks.grid(row=count, column=2, padx=(10,0))
option1 = Radiobutton(self.test_frame, text=q['option1'], variable=option, value=q['option1'], command=lambda: self.append_answer(q['qno'],option.get(),'objective'))
option1.grid(row=count+1, column=1, padx=20)
option2 = Radiobutton(self.test_frame, text=q['option2'], variable=option, value=q['option2'], command=lambda: self.append_answer(q['qno'],option.get(),'objective'))
option2.grid(row=count+1, column=2, padx=20)
option3 = Radiobutton(self.test_frame, text=q['option3'], variable=option, value=q['option3'], command=lambda: self.append_answer(q['qno'],option.get(),'objective'))
option3.grid(row=count+2, column=1, padx=20)
option4 = Radiobutton(self.test_frame, text=q['option4'], variable=option, value=q['option4'], command=lambda: self.append_answer(q['qno'],option.get(),'objective'))
option4.grid(row=count+2, column=2, padx=20)
option5 = Radiobutton(self.test_frame, text="None", variable=option, value="None", command=lambda: self.append_answer(q['qno'],option.get(),'objective'))
option5.grid(row=count+1, column=3, padx=20)
count+=3
k+=1
root = Tk()
test_page(root,'mydata.json')
root.mainloop()
Page looks something like this-

How to make the GUI using Tkinter of the format specified below?

In the code given below, I want to create a GUI in such a way that 1) In the top frame: 1st row : "x"(checkbutton), right to "x"(checkbutton) must be "Choose xFile"(button) and right to "Choose xFile"(button) must be "clear"(button) and likewise in the 2nd row : for "y". Only when the "x" checkbutton is checked, the "choose xFile" button should gets enabled. And when clicked upon "choose xFile", it has to open the file dialogbox. And the choosen file contents should get displayed using "description box" below the "Input data"(label) in the middle frame(with both horizontal and vertical scrollbar). And when "clear" button is clicked, only the selected file contents(x or y file choosen) in the "description box" must be cleared and it must enable the "Choose xFile"(button) or "Choose yFile"(button) to perform the task again(i.e to open the file dialog box). Below to the "description box" must contain "Reset" button and to the right of "Reset" button must be "Submit" button in the middle portion of the middle frame. When the "Reset" button is clicked, all the contents being displayed in the "description box" must be cleared and the all the checkboxes must be unchecked so that the user can perform the selection process again. In the bottom frame, below "Model Output"(label), a "description box" along with "horizontal" and "vertical" scrollbar must be there. Below to the "description box" must contain "Exit" button which is placed in the middle of the "bottom frame".
from tkinter import *
def forButton1():
filename1 = askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton2():
filename1 = askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton7():
root.destroy()
root = Tk()
root.title("Spatialization of DSSAT")
topFrame = LabelFrame(root, text = "Select input file")
MyVar1 = IntVar()
MyVar2 = IntVar()
MyCheckbutton1 = Checkbutton(topFrame, text="x", variable=MyVar1)
#MyCheckbutton1.grid(row=0, column=0)
MyCheckbutton1.pack()
MyCheckbutton2 = Checkbutton(topFrame, text="y", variable=MyVar2)
#MyCheckbutton2.grid(row=1, column=0)
MyCheckbutton2.pack()
Button1 = Button(topFrame, text = "Choose xFile", command = forButton1)
#button1.grid(row=0, column=1)
Button1.pack()
Button2 = Button(topFrame, text = "Choose yFile", command = forButton2)
#button2.grid(row=0, column=1)
Button2.pack()
Button3 = Button(topFrame, text = "Clear")
Button3.pack()
Button4 = Button(topFrame, text = "Clear")
Button4.pack()
topFrame.pack(side=TOP)
middleFrame = Frame(root)
label1 = Label(middleFrame, text = "Input data:")
label1.grid(row = 4)
scrollbar = Scrollbar(middleFrame)
myList = Listbox(middleFrame, yscrollcommand = scrollbar.set)
myList.pack()
scrollbar.config( command = myList.yview )
scrollbar.pack()
Button5 = Button(middleFrame, text = "Reset")
#button1.grid(row=0, column=1)
Button5.pack()
Button6 = Button(middleFrame, text = "Submit")
#button1.grid(row=0, column=1)
Button6.pack()
middleFrame.pack()
bottomFrame = Frame(root)
label2 = Label(bottomFrame, text = "Model Output:")
label2.grid(row = 10)
Button7 = Button(bottomFrame, text = "Exit", command = forButton7)
#button1.grid(row=0, column=1)
Button7.pack()
bottomFrame.pack()
root.geometry("500x500")
root.mainloop()
Here I have fixed the placement of the given widgets (not their functionalities) as asked in question. You can follow this way to get your desired format:
from tkinter import *
from tkinter import filedialog
def forButton1():
filename1 = filedialog.askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton2():
filename1 = filedialog.askopenfilename()
with open(filename1) as f:
for i in f:
myList.insert(END, i)
print(filename1)
def forButton7():
root.destroy()
root = Tk()
root.title("Spatialization of DSSAT")
root.grid_columnconfigure(0, weight=1)
topFrame = LabelFrame(root, text="Select input file")
topFrame.grid(row=0, column=0, padx=8, pady=8, sticky=N+E+S+W)
topFrame.grid_rowconfigure(0, weight=1)
topFrame.grid_rowconfigure(1, weight=1)
topFrame.grid_columnconfigure(0, weight=1)
topFrame.grid_columnconfigure(1, weight=1)
topFrame.grid_columnconfigure(2, weight=1)
middleFrame = LabelFrame(root, text="Input data")
middleFrame.grid(row=1, column=0, padx=8, pady=8, sticky=N+E+S+W)
middleFrame.grid_rowconfigure(0, weight=1)
middleFrame.grid_rowconfigure(1, weight=0)
middleFrame.grid_columnconfigure(0, weight=1)
middleFrame.grid_columnconfigure(1, weight=1)
bottomFrame = LabelFrame(root, text="Model Output")
bottomFrame.grid(row=2, column=0, padx=8, pady=8, sticky=N+E+S+W)
bottomFrame.grid_rowconfigure(0, weight=1)
bottomFrame.grid_columnconfigure(0, weight=1)
MyVar1 = IntVar()
MyVar2 = IntVar()
MyCheckbutton1 = Checkbutton(topFrame, text="x", variable=MyVar1)
MyCheckbutton1.grid(row=0, column=0, padx=4, pady=4)
Button1 = Button(topFrame, text="Choose xFile", command=forButton1)
Button1.grid(row=0, column=1, padx=4, pady=4)
Button3 = Button(topFrame, text="Clear")
Button3.grid(row=0, column=2, padx=4, pady=4)
MyCheckbutton2 = Checkbutton(topFrame, text="y", variable=MyVar2)
MyCheckbutton2.grid(row=1, column=0, padx=4, pady=4)
Button2 = Button(topFrame, text="Choose yFile", command=forButton2)
Button2.grid(row=1, column=1, padx=4, pady=4)
Button4 = Button(topFrame, text="Clear")
Button4.grid(row=1, column=2, padx=4, pady=4)
innerMiddleFrame = Frame(middleFrame)
innerMiddleFrame.grid(row=0, column=0, columnspan=2, padx=4, pady=4)
innerMiddleFrame.grid_columnconfigure(0, weight=1)
innerMiddleFrame.grid_columnconfigure(1, weight=0)
scrollbar = Scrollbar(innerMiddleFrame)
myList = Listbox(innerMiddleFrame, yscrollcommand=scrollbar.set)
myList.grid(row=0, column=0, sticky=N+E+S+W)
scrollbar.config(command=myList.yview)
scrollbar.grid(row=0, column=1, sticky=N+E+S+W)
Button5 = Button(middleFrame, text="Reset")
Button5.grid(row=1, column=0, padx=4, pady=4)
Button6 = Button(middleFrame, text="Submit")
Button6.grid(row=1, column=1, padx=4, pady=4)
Button7 = Button(bottomFrame, text="Exit", command=forButton7)
Button7.grid(row=0, column=0, padx=4, pady=4)
root.geometry("250x425")
root.mainloop()

why is nothing returned when I invoke .get()

Having trouble setting the file path that is selected by the user and setting to variable. I am able to retrieve the path and set it to display in the entry box but I would like to capture that path and import it into another script. Maybe my logic is flawed here? What am I doing wrong?
import Tkinter
import tkFileDialog
from Tkinter import *
from tkFileDialog import *
class GUI:
def __init__(self, master):
self.master = master
master.title("XML Compare Tool")
master.geometry('700x300')
path1 = StringVar()
path2 = StringVar()
self.bb1 = Button(master, text="Browse", command=lambda: path1.set(askopenfilename()))
self.bb1.grid(row=0, column=0, padx=5, pady=5)
self.bb2 = Button(master, text="Browse", command=lambda: path2.set(askopenfilename()))
self.bb2.grid(row=1, column=0, padx=5, pady=5)
self.confirm = Button(master, text="Confirm", command='')
self.confirm.grid(row=3, column=1, padx=5, pady=5, sticky='')
self.entry1 = Entry(master, width=75, textvariable=path1)
self.entry1.grid(row=0, column=1, columnspan=2, sticky=W)
print path1.get()
self.entry2 = Entry(master, width=75, textvariable=path2)
self.entry2.grid(row=1, column=1, sticky=W)
self.t_label = Label(master, text="Script Output")
self.t_label.grid(row=4, column=1, columnspan=1, sticky='')
self.t_frame = Frame(master, bg="white", height=150, width=600)
self.t_frame.grid(row=5, column=1, columnspan=1, sticky='')
self.t_text = Text(self.t_frame)
root = Tk()
my_gui = GUI(root)
root.mainloop()
You do not need to use a textvariable, you can just use variable = entry1.get(). A Tkinter textvariable is not like a traditional python variable, it is just used for setting the text in an entry.

Categories

Resources