I am trying to enter variables to use in an API call app. When I use the normal python input thing, it works, but I want to be able to enter these via the entry widget with tkinter. When I try to use the tkinter entry widget, it does not work.
`
import API_test
import tkinter
from tkinter import *
import random
import requests
from tkinter.ttk import *
##root for Tkinter app
root = tkinter.Tk()
root.geometry("1000x400")
with open('inputfile.txt', 'w') as fp:
fp.truncate(0)
stringvar1 = tkinter.StringVar()
stringvar2 = tkinter.StringVar()
##tkinter labels
L1 = tkinter.Label(text="Enter City ",)
L2 = tkinter.Label(text = "Enter Cuisine ",)
##tkinter entry
rootentry1 = tkinter.Entry(root, textvariable=stringvar1)
rootentry2 = tkinter.Entry(root, textvariable=stringvar2)
###buttoninput
##tkinter button
##tkinter label and entry placement
L1.grid(row=0,column=0)
rootentry1.grid(row=0,column=1)
L2.grid(row=1,column=0)
rootentry2.grid(row=1,column=1)
def inputfile():
locvarentry = stringvar1
catvarentry = stringvar2
with open('inputfile.txt', 'w') as fw:
fw.write(str(locvarentry) ",")
fw.write(str(catvarentry) ",")
B1 = tkinter.Button( root,rtext="Submit", command=inputfile)
B1.grid(row=3, column=0)
The rest of the program has an API call using yelp in a different module and returns a display saying that the number of restaurants in is . I have that covered, but I am not sure how to handle the input. `
I tried playing around with variables, using .get() and .set() in different combinations.
CodePudding user response:
import tkinter
##root for Tkinter app
root = tkinter.Tk()
root.geometry("1000x400")
stringvar1 = tkinter.StringVar()
stringvar2 = tkinter.StringVar()
##tkinter labels
L1 = tkinter.Label(text="Enter City ",)
L2 = tkinter.Label(text = "Enter Cuisine ",)
##tkinter entry
rootentry1 = tkinter.Entry(root)
rootentry2 = tkinter.Entry(root)
###buttoninput
##tkinter button
##tkinter label and entry placement
L1.grid(row=0,column=0)
rootentry1.grid(row=0,column=1)
L2.grid(row=1,column=0)
rootentry2.grid(row=1,column=1)
def inputfile():
locvarentry = rootentry1.get()
catvarentry = rootentry2.get()
print(str(locvarentry) ",")
print(str(catvarentry) ",")
B1 = tkinter.Button( root, text="Submit", command=inputfile)
B1.grid(row=3, column=0)
root.mainloop()
What do you think of this? I removed the opening of the file for making the testing easier, so just implement this if it works...
CodePudding user response:
Code works for me when I use .get()
in
locvarentry = stringvar1.get()
catvarentry = stringvar2.get()
And it needs root.mainloop()
to work.
Full code:
import API_test
import tkinter
from tkinter import *
import random
import requests
from tkinter.ttk import *
##root for Tkinter app
root = tkinter.Tk()
root.geometry("1000x400")
with open('inputfile.txt', 'w') as fp:
fp.truncate(0)
stringvar1 = tkinter.StringVar()
stringvar2 = tkinter.StringVar()
##tkinter labels
L1 = tkinter.Label(text="Enter City ",)
L2 = tkinter.Label(text="Enter Cuisine ",)
##tkinter entry
rootentry1 = tkinter.Entry(root, textvariable=stringvar1)
rootentry2 = tkinter.Entry(root, textvariable=stringvar2)
###buttoninput
##tkinter button
##tkinter label and entry placement
L1.grid(row=0,column=0)
rootentry1.grid(row=0,column=1)
L2.grid(row=1,column=0)
rootentry2.grid(row=1,column=1)
def inputfile():
locvarentry = stringvar1.get()
catvarentry = stringvar2.get()
print(locvarentry, catvarentry)
with open('inputfile.txt', 'w') as fw:
fw.write(str(locvarentry) ",")
fw.write(str(catvarentry) ",")
B1 = tkinter.Button(root, text="Submit", command=inputfile)
B1.grid(row=3, column=0)
root.mainloop()
EDIT:
The same code but little cleaner:
# PEP8: first built-in modules, next external modules, next your modules.
#import random
import tkinter as tk # PEP8: `import *` not preferred
#import tk.ttk as ttk # PEP8: `import *` not preferred
#import requests
#import API_test
# --- functions --- # PEP8: all functions before main code
def inputfile():
with open('inputfile.txt', 'w') as fw: # it will truncate file automatically
fw.write(stringvar1.get() "," stringvar2.get() ",")
#root.destroy() # if you want to close window after writing file
# --- main ---
#with open('inputfile.txt', 'w') as fp: # it will truncate file automatically even if you use `pass` instead of `truncate`
# fp.truncate(0)
# ---
root = tk.Tk()
root.geometry("1000x400")
stringvar1 = tk.StringVar(root) # it is good to set `master` for all objects in `tkinter`
stringvar2 = tk.StringVar(root) # it is good to set `master` for all objects in `tkinter`
L1 = tk.Label(text="Enter City ")
L2 = tk.Label(text="Enter Cuisine ")
rootentry1 = tk.Entry(root, textvariable=stringvar1)
rootentry2 = tk.Entry(root, textvariable=stringvar2)
L1.grid(row=0, column=0) # PEP8: space after comma
rootentry1.grid(row=0, column=1) # PEP8: space after comma
L2.grid(row=1, column=0) # PEP8: space after comma
rootentry2.grid(row=1, column=1) # PEP8: space after comma
B1 = tk.Button(root, text="Submit", command=inputfile)
B1.grid(row=3, column=0)
root.mainloop()