Python can't get the .get function to work [duplicate] - python

This question already has answers here:
Error when configuring tkinter widget: 'NoneType' object has no attribute
(2 answers)
Closed 6 years ago.
Lurked around the forums but can't seem to get the get() function to works, it keeps returning that it is not defined. Can somebody point out what I did wrong?
from Tkinter import *
the_window = Tk()
def button_pressed ():
content = entry.get()
if (content == '1'):
print 'lol'
else:
print 'boo'
entry = Entry(master=None, width = 8, bg='grey').grid(row=2, column = 2)
button = Button(master=None, height=1, width=6, text='Go!', command=button_pressed).grid(row=2, pady=5, column=3)
the_window.mainloop()

The grid method returns None. This assigns a None value to entry.
Instead, you want to assign that instance of Entry to entry and then modify the grid:
entry = Entry(master=None, width = 8, bg='grey')
entry.grid(row=2, column = 2)

entry = Entry(master=None, width = 8, bg='grey').grid(row=2, column = 2)
This will assign entry to the return value of the .grid() method, but .grid() does not return anything, so entry will be None.
You should instead write
entry = Entry(master=None, width=8, bg='grey')
entry.grid(row=2, column=2)
Do the same for all other widgets.

Related

Why binding a key to Entry widget doesn't work? [duplicate]

This question already has an answer here:
Basic query regarding bindtags in tkinter
(1 answer)
Closed 10 months ago.
HI guys i'm trying to simply create a text space that replaces the letters or any input into asterisks just like happen with the regular passwords. But why if i bind any key to the function "text" if i press any key the first input is empty? Becuase if i type "m" when in the function i print(e.get()) the output is empty in the console? Why it ignores the input and how to simply show the asterisk in the text area and bind the normal password to the variable "a" just like in my code? With e.delete and e.insert i simply cut the last character and replace it with asterisk here is my code
from tkinter import *
a = ""
window = Tk()
def entra():
global a
if a == "cgf":
print ("a")
else:
print("noo")
def text (event):
global a
a = a + e.get()
print(e.get())
e.delete(len(e.get()) - 1)
e.insert(END, "*")
e = Entry(window, borderwidth=3, width=50, bg="white", fg="black")
e.place(x=100, y=35)
e.bind("<Key>", text)
bottone_entra = Button (window, text = "Entra", borderwidth=2.5,command = entra)
bottone_entra.place (x=100, y=65)
etichetta = Label(window, text = "PASSWORD", bg = "#2B2B2B", fg = "white")
etichetta.place(x=97.5, y=10)
window.geometry ("500x500")
window.title ("Password")
window.configure(bg = "#2B2B2B")
window.mainloop()
You don't have to manually build a logic for it, tkinter luckily already provides it. You just have to set an extra option to the entry widget, show='*':
e = Entry(window, borderwidth=3, width=50, bg="white", fg="black", show='*')
There are many reasons as to why you should avoid building/using this logic of your own, to begin with, once you do get(), you will only get **** and not whatever the text originally was.

Getting inputs from comboboxes doesn't work [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
I made a couple of combo-boxes, where the User can choose between pre-defined settings:
# create comboboxes for settings
combobox_sensortype = ttk.Combobox(root, values=('RGB','MS'), state='readonly').grid(row=3,column=2)
combobox_dem_dop = ttk.Combobox(root, values=('DEM','DOP'), state='readonly').grid(row=4,column=2)
combobox_sensortype_def = ttk.Combobox(root, values=('RGB_HZ','RGB_MR','MS_HZ','MS_MR'), state='readonly').grid(row=5,column=2)
combobox_run_SAGA_bat = ttk.Combobox(root, values=('Yes','No'), state='readonly').grid(row=6,column=2)
# getters for extracting user input from comboboxes
sensortype = combobox_sensortype.get()
demdop = combobox_dem_dop.get()
sensortype_def = combobox_sensortype_def.get()
run_SAGA_bat = combobox_run_SAGA_bat.get()
Unfortunately, I get this:
AttributeError: 'NoneType' object has no attribute 'get'
Whats the issue here?
I really hope someone can help.
This is because you called the method .grid(row=3,column=2). This doesn't return anything, thus the variable's value was set to None. To get your result, put them in separate lines. So you should define the Combobox, then grid it.
Solution:
import tkinter.ttk as ttk
from tkinter import *
root = Tk()
# create comboboxes for settings
combobox_sensortype = ttk.Combobox(root, values=('RGB', 'MS'), state='readonly') # define the combobox
combobox_sensortype.grid(row=3, column=2) # grid it!
combobox_dem_dop = ttk.Combobox(root, values=('DEM', 'DOP'), state='readonly')
combobox_dem_dop.grid(row=4, column=2)
combobox_sensortype_def = ttk.Combobox(root, values=('RGB_HZ', 'RGB_MR', 'MS_HZ', 'MS_MR'), state='readonly')
combobox_sensortype_def.grid(row=5, column=2)
combobox_run_SAGA_bat = ttk.Combobox(root, values=('Yes', 'No'), state='readonly')
combobox_run_SAGA_bat.grid(row=6, column=2)
# getters for extracting user input from comboboxes
sensortype = combobox_sensortype.get()
demdop = combobox_dem_dop.get()
sensortype_def = combobox_sensortype_def.get()
run_SAGA_bat = combobox_run_SAGA_bat.get()
root.mainloop()

How do I call an entry on tkinter? [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
I'm new to Python and I'm struggling to figure out what's wrong with my code.
import tkinter as tk
r = tk.Tk()
r.title('PROJECT')
def file_entry():
global ent
tk.Label(r, text = "ENTRY").grid(row = 2)
ent = tk.Entry(r).grid(row = 2, column = 1)
b_ent = tk.Button(r, text="OK", width = 5, command = print_input).grid(row = 2, column = 2)
def print_input():
print(ent.get())
l = tk.Label(r, text="OPTION 1 OR 2?").grid(row = 0)
b = tk.Button(r, text='1', width=25, command=file_entry).grid(row = 1,column = 0)
b2 = tk.Button(r, text='2', width=25, command=r.destroy).grid(row = 1,column = 1)
r.mainloop()
I'm trying to call "ent", but every time I run it, it would give me an error for it being a NoneType.
Like I said, I'm new to Python, and this is a school project, it would really be helpful if someone could explain what the problem is, as simply as possible.
Thanks!
It appears that chaining the grid to the entry widget is not acceptable.
If you put it on a separate line, the ent variable get properly assigned and get the inputted value as opposed to not working and being assigned None.
Try this:
def file_entry():
global ent
tk.Label(r, text="ENTRY").grid(row=2)
ent = tk.Entry(r)
ent.grid(row=2, column=1)
b_ent = tk.Button(r, text="OK", width=5, command=print_input).grid(row=2, column=2)

Tkinter Entry Field Capture AttributeError: [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 5 years ago.
I want to capture the data in the entry field from the second window defined under output. When I click submit get the following message: AttributeError: 'NoneType object has no attribute 'get'.
I feel like this should be an easy fix and do not understand why can't I capture the data from the entry field?
from tkinter import *
import xlsxwriter
class MyFirstGUI:
def __init__ (self, master):
master.title("Main")
master.geometry("400x400+230+160")
button1 = Button(master, text="1", command= self.output).grid(row=0, column=0)
def output(self):
cw1= Toplevel(root)
cw1.title("cw1")
cw1.geometry("400x300+160+160")
self.b2 = Button(cw1, text="Submit",command = self.write_to_xlsx).grid(row=0, column=2)
self.l2 = Label(cw1, text="New Specimen").grid(row=0, column=0)
self.e2 = Entry(cw1).grid(row=0, column=1)
def write_to_xlsx(self):
workbook = xlsxwriter.Workbook('tkintertest19.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_string('C1', self.e2.get())
workbook.close()
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()
What you need to do is split the line
self.l2 = Label(cw1, text="New Specimen").grid(row=0, column=0)
into
self.l2 = Label(cw1, text = "New Specimen")
self.l2.grid(row=0, column=0)
Non-intuitive as this may seem, the grid/pack/place functions return None, so the whole shebang (Label().grid()) returns None. The solution is simply to split it up so that you get the right thing when you use .get().

What should I be using instead of .get() [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 8 years ago.
Using python I wrote the code below. I am trying to make a small calculator which multiplies an input by 5. When using the .get() command, I get 'NoneType' object has no attribute 'get'.
Can anyone help?
from Tkinter import *
def calc_handler():
question = entry.get()
answer = question * 5
print answer
main = Tk()
main.title('Calculator')
main.geometry('350x100+300+100')
instructions = Label(main, text='Input A Number And I Will Multiply It By 5').grid(row=0, columnspan=2)
entry = Entry(main).grid(row=1, columnspan=2)
enter = Button(main, text='Click Here To Calculate', command=calc_handler).grid(row=3, column=1)
clear = Button(main, text='Clear').grid(row=3, column=2)
mainloop()
It is hard to define why it happens (I reckon grid return a None object) but change this line:
entry = Entry(main).grid(row=1, columnspan=2)
To:
entry = Entry(main)
entry.grid(row=1, columnspan=2)
And the reason why it never actually multiplies is that because question is a string thus you need to convert it to a integer by using the int() function before multiplying it:
def calc_handler():
question = entry.get()
answer = int(question) * 5
print answer

Categories

Resources