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
Related
This question already has an answer here:
Tkinter assign button command in a for loop with lambda [duplicate]
(1 answer)
Closed 1 year ago.
I'm trying to make a little file explorer with just buttons, I'm still in the early stages and want to make a function prints which button was pressed and came up with this:
import tkinter as tk
buttons = []
window = tk.Tk()
window.geometry("200x100")
def open(button):
print(button)
def list(titles):
i=0
while i<(len(titles)):
btn = tk.Button(text=titles[i], width=20, command=lambda: open(i))
buttons.append(btn)
buttons[i].grid(row=i, column=1)
print(f"adding {titles[i]}")
i=i+1
list(["title1", "title2", "title3"])
window.mainloop()
There's one problem: It always prints 3. I think I know what the problem is, i always stays 3 after generating the button so it passes 3 to the function, but I don't know how to solve it.
I used the lambda cuz I cant pass parameters just using open(i) and found the lambda-solution to that on this question .
Can someone help?
Tnx!
Because it over writes the commands on one button when you assign it again. Do:
import tkinter as tk
buttons = []
window = tk.Tk()
window.geometry("200x100")
def open(button):
print(button)
def list(titles):
btn = tk.Button(text=titles[0], width=20, command=lambda: open(1))
buttons.append(btn)
buttons[0].grid(row=1, column=1)
print(f"adding {titles[0]}")
btn = tk.Button(text=titles[1], width=20, command=lambda: open(2))
buttons.append(btn)
buttons[1].grid(row=2, column=1)
print(f"adding {titles[1]}")
btn = tk.Button(text=titles[2], width=20, command=lambda: open(2))
buttons.append(btn)
buttons[2].grid(row=3, column=1)
print(f"adding {titles[2]}")
list(["title1", "title2", "title3"])
window.mainloop()
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 have a problem, I want to make a fontions which opens a site when I press the button except that as soon as I launch the file the functions are executed by themselves
from tkinter import *
import random
import string
import webbrowser
def Tokens():
webbrowser.open_new("https://xlean.me")
button = "Start"
windows = Tk()
windows.title("Discord Tokens")
windows.geometry("500x150")
windows.minsize(500,150)
windows.maxsize(500,150)
windows.iconbitmap("icon.ico")
windows.config(background="#242424")
text = Label(windows, text="Hello to you !", bg="#242424", fg='white')
text.config(font=("Alatsi", 30))
text.pack()
button = Button(windows, text=button, bg='#202020', fg='white', command=Tokens())
button.pack(fill=X)
windows.mainloop()
You should pass the Tokens function as the command arg, while you are passing the result of its execution instead. You need to remove the parenthesis after command=Tokens. Here is the fixed code:
from tkinter import *
import random
import string
import webbrowser
def Tokens():
webbrowser.open_new("https://xlean.me")
button = "Start"
windows = Tk()
windows.title("Discord Tokens")
windows.geometry("500x150")
windows.minsize(500,150)
windows.maxsize(500,150)
windows.iconbitmap("icon.ico")
windows.config(background="#242424")
text = Label(windows, text="Hello to you !", bg="#242424", fg='white')
text.config(font=("Alatsi", 30))
text.pack()
button = Button(windows, text=button, bg='#202020', fg='white', command=Tokens)
button.pack(fill=X)
windows.mainloop()
This is my first time coding in tkinter. When I try to create a new button in the function 'Registering' i keep getting the same error 'Button' object is not callable. I don't understand what this error is suggesting about the simple code I have written. Can anyone clarify this for me in the context of the code below?
from tkinter import *
root = Tk()
def Registering():
window = Toplevel(root)
login_button = Button(window, width = 120, height = 42)
Button = Button(root,text= "Enter",command=Registering)
Button.pack()
root.mainloop()
Button = Button(root,text= "Enter",command=Registering)
Button.pack()
By doing Button = Button (... you override tkinter's definition of Button.
Use a different (hopefully more meaningful) name:
register_button = Button(root,text= "Enter",command=Registering)
register_button.pack()
the reason its showing the error is cause ur using Button as ur variable name
from tkinter import *
root = Tk()
def Registering():
window = Toplevel(root)
login_button = Button(window, width = 120, height = 42)
btn= Button(root,text= "Enter",command=Registering)
btn.pack()
root.mainloop()
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().
This question already has an answer here:
How do I create child windows with Python tkinter?
(1 answer)
Closed 6 years ago.
When you run this code and try and check the the check button and click the button to print out the value of the check button it does not work. I can't figure out why. It prints out 0 whether checked or unchecked.
from tkinter import *
def awesome():
def click_me():
print(var.get())
return
root = Tk()
root.title("a good try")
var = IntVar()
x = Checkbutton(root, text = "check me", variable = var)
y = Button(root, text = "click me", command = click_me)
x.pack()
y.pack()
root.mainloop()
return
def main():
main = Tk()
cool = Button(main, text = "click", command = awesome)
cool.pack()
main.mainloop()
main()
change root = Tk() to root = Toplevel()
need to use Toplevel() for a window that opens on another window.