How to use get() method in Tkinter? [duplicate] - python

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
I've been trying to get a very simple text entry box and button going, but the following code creates an AttributeError when trying to get the value from self.entry, which is apparently a NoneType.
from tkinter import *
class Screen1(Frame):
def __init__(self, master):
super().__init__(master)
self.grid()
self.entry = Entry(self).grid(row=0, column=0)
self.bttn = Button(self,
text="Enter",
command=self.get_entry
).grid(row=0, column=1)
def get_entry(self):
message = self.entry.get()
print(message)
root = Tk()
root.geometry("400x100")
Screen1(root)
root.mainloop()
How do I fix this? Thanks

grid method returns None and you are setting that equal to self.entry
Change the following:
self.entry = Entry(self).grid(row=0, column=0)
# to
self.entry = Entry(self)
self.entry.grid(row=0, column=0)

Related

How can i get and save a value from an entry if i use grid?(tkinter) [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
How can i get a value from an entry if i use grid?
from tkinter import *
window = Tk()
window.title("Test")
window.geometry(600x600)
window.config()
e = Entry(window, width=20).grid(row=0, column=1)
entry = e.get()
print(entry)
Move the .grid() down on a separate line
entry = Entry(window, width=20)
entry.grid(row=0, column=1)
print(entry)
BTW, you should have searched this site. I am sure there are millions like this

Why won't text in tkinter change? [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
I am having a trouble changing text in python even though though that I saw a lot of use it
My code:
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text).pack()
window.mainloop()
You assigned the return value of .pack() to btn, and pack doesn't return the widget it was called on (it returns None implicitly, since it has no useful return value). Just split up the creation of the button from the packing:
from tkinter import *
window = Tk()
def change_text():
btn.config(text="second text")
btn = Button(window, text="first text", command=change_text)
btn.pack()
window.mainloop()
your code is good but it is not good to make .pack() in the same line
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text )
btn.pack()
window.mainloop()
it works just will

Manipulation of a button property by its own command callback function [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
I implemented the following code in python to manipulate an own property (here button.text) of the pressed button.
When executing the code I get the following error: "AttributeError: 'Gui' object has no attribute 'button'". Important for my example is, that the button is part of the class and created in its init. Other working examples with a global defined button I found and got running.
#!/usr/bin/python3
# -*- coding: iso-8859-1 -*-
import tkinter
from tkinter import *
from tkinter import ttk
class Gui:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.frame.pack(fill='both', side='top', expand='True')
self.button = Button(master=self.frame,
text='connect',
height=20,
width=80,
padx=1,
pady=1,
command=self.connect_disconnct(),
compound=LEFT)
self.button.grid(row=0, column=0, padx=5, pady=5, sticky='ew')
mainloop()
def connect_disconnct(self):
if self.button.test == connect:
print("Button pressed -> connect")
self.button.text = "disconnect"
else:
print("Button pressed -> disconnect")
self.button.text = "connect"
if __name__ == '__main__':
form = Tk()
Gui(form)
form.mainloop()
How the own button element can be passed to the callback function so for example the text of the calling object can be changed in the callback function?
Remove the () in this line.
command=self.connect_disconnct(),
You want to hand the button a function to call. The () calls the function, executing it before the attribute self.button is set. Even if there were no reference to self.button in the method you would be handing Button the return value of the method, which in this case is None.
There are some additional things wrong with this code. To get the button text, you need to do this self.button['text'] == 'connect'.
Additionally, you cannot set the text in this way. You need to use the configure() method.
self.button.configure(text="disconnect")
Also, you have one too many calls to mainloop().
Full code:
import tkinter
from tkinter import *
from tkinter import ttk
class Gui:
def __init__(self, master):
self.master = master
self.frame = Frame(self.master)
self.frame.pack(fill='both', side='top', expand='True')
self.button = Button(master=self.frame,
text='connect',
height=20,
width=80,
padx=1,
pady=1,
command=self.connect_disconnct,
compound=LEFT)
self.button.grid(row=0, column=0, padx=5, pady=5, sticky='ew')
def connect_disconnct(self):
if self.button['text'] =='connect':
print("Button pressed -> connect")
self.button.configure(text="disconnect")
else:
print("Button pressed -> disconnect")
self.button.configure(text="connect")
if __name__ == '__main__':
form = Tk()
Gui(form)
form.mainloop()
In addition to #Axe319 answer there is a few things:
You are using the wrong syntax to change the button text, the correct syntax is:
self.button.config(text = "disconnect")
As for getting text from the button:
self.button['text'] == 'connect':
Finally there is a spelling error:
self.button.test == connect:
# x

Tkinter Entry .get from a definition [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 2 years ago.
I keep getting a none Type attribute error and idk how to fix it as i looked on many videos and none helped. i don't know what I'm doing wrong. any help is appreciated!
from tkinter import *
def find():
user = whiteBox.get()
print(user)
root = Tk()
weatherResult = StringVar()
weatherResult.set("Enter a Place")
weather = Label(root, textvariable=weatherResult).pack()
whiteBox = Entry(root).pack()
check = Button(root, text="find", command=find).pack()
root.mainloop()
You are making a very common mistake. The value of your widget will be a NoneType, because you are using .pack on the same line.
So, your code:
from tkinter import *
def find():
user = whiteBox.get()
print(user)
root = Tk()
weatherResult = StringVar()
weatherResult.set("Enter a Place")
weather = Label(root, textvariable=weatherResult).pack()
whiteBox = Entry(root)
whiteBox.pack()
check = Button(root, text="find", command=find).pack()
root.mainloop()
This should be the result you want. Hope this helps!

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

Categories

Resources