This question already has an answer here:
(Python) How to limit an entry box to 2 characters max [duplicate]
(1 answer)
Closed 5 years ago.
How do I limit the input on a Entry to only 4 characters
from tkinter import *
window = Tk()
display = Entry(window)
display.grid()
You can do this by running a trace on the attribute textvariable of the entry widget. Whenever this variable is updated you will need to set the variable to it's own value up to the 4th character.
See below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.sv = StringVar()
self.entry = Entry(root, textvariable = self.sv)
self.entry.pack()
self.sv.trace("w", lambda name, index, mode, sv=self.sv: self.callback(self.sv))
def callback(self, sv):
self.sv.set(self.sv.get()[:4])
root = Tk()
App(root)
root.mainloop()
Related
This question already exists:
Entry widget in tkinter
Closed 1 year ago.
So I made a simple program however it doesn't seem to work
my code is:
e = Entry(root, font = 20,borderwidth=5)
e.grid(row=1)
def capture(event):
print(e.get())
e.bind("<Key>", capture)
However the first time I enter something in the box, all I get is an empty string.
As #Art stated:
You can use "<KeyRelease>", e.bind("<Key>", lambda event: e.after(1, capture, event))" or simply Use StringVar()
from tkinter import *
root=Tk()
e = Entry(root, font = 20,borderwidth=5)
e.grid(row=1)
def capture(event):
print(e.get())
e.bind("<Key>", lambda event: e.after(1, capture, event))
root.mainloop()
Or you can use a StringVar()
from tkinter import *
root=Tk()
s=StringVar()
e = Entry(root,textvariable=s, font = 20,borderwidth=5)
e.grid(row=1)
def capture(*args):
print(s.get())
s.trace("w",capture)
root.mainloop()
This question already has answers here:
How to reconfigure tkinter canvas items?
(2 answers)
Closed 1 year ago.
While learning Tkinter, I got a problem while creating a function for a button.
I need to config a canvas text in function. But I don't know how to do it.
canvas.create_text(700,350,font=("Arial Bold", 35),fill='white')
def clicked():
res = "THANKYOU " + txt.get()
res1.config(text=res)
I want that res1 to be that canvas.create_text()
You can make use of canvas.itemconfig/canvas.itemconfigure to change the properties of items on canvas.
minimal example:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, bg="black")
canvas.pack()
text = canvas.create_text((100,50),text="Hello", fill='white')
def clicked():
res = "THANKYOU "
canvas.itemconfig(text, text=res)
tk.Button(root, text="click", command=clicked).pack()
root.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 1 year ago.
I'm working on a custom button for a list, and want to make it change color when the mouse hovers above the button. See the following code for how I'm trying to do it:
import tkinter as tk
from tkinter import *
class list_button:
def __init__(self, container, text, font_size):
self.list_button = tk.Button(container, text=text, font = ("helvatica", (0 + font_size)), relief = "flat")
self.list_button.pack()
self.list_button.bind('<Enter>', self.enter_func(self))
self.list_button.bind('<Leave>', self.leave_func(self))
def enter_func(self, key):
print("Enter")
self.list_button.configure(bg = "grey")
def leave_func(self, key):
print("Leave")
self.list_button.configure(bg = "white")
root = tk.Tk()
for i in range(10):
list_button(root, i, 15)
root.mainloop()
What seems to happen is that the code calls the function once and then unbinds the function.
What am I doing wrong here?
For people that are stuck on the same problem. This is the revised code:
import tkinter as tk
from tkinter import *
class list_button:
def __init__(self, container, text, font_size):
self.list_button = tk.Button(container, text=text, font = ("helvatica", (0 + font_size)), relief = "flat")
self.list_button.pack()
self.list_button.bind('<Enter>', self.enter_func)
self.list_button.bind('<Leave>', self.leave_func)
def enter_func(self, key):
print("Enter")
self.list_button.configure(bg = "grey")
def leave_func(self, key):
print("Leave")
self.list_button.configure(bg = "white")
root = tk.Tk()
for i in range(10):
list_button(root, i, 15)
root.mainloop()
The changes are in the following part:
self.list_button.bind('<Enter>', self.enter_func)
self.list_button.bind('<Leave>', self.leave_func)
This question already has an answer here:
How to update label on tkinter
(1 answer)
Closed 2 years ago.
i am want to create a button that shows you how many times you have pressed it while you are pressing it.
But i don't know how to "refresh" it everytime it's pressed .
Can someone help me ?
from tkinter import *
class Press():
def __init__(self):
self.root = Tk()
self.scale = self.root.wm_minsize(width=500, height=300)
self.changer = StringVar()
self.changer.set(0)
self.label = Label(self.root , textvariable= self.changer)
self.button = Button(self.root, text="PRESS", command= self.amount_pressed)
self.button.pack()
self.label.pack()
self.mainloop = mainloop()
def amount_pressed(self):
self.changer.set(+1)
test = Press()
Try this:
from tkinter import *
class Press():
def __init__(self):
self.root = Tk()
self.scale = self.root.wm_minsize(width=500, height=300)
self.changer = StringVar()
self.changer.set(0)
self.label = Label(self.root , textvariable= self.changer)
self.button = Button(self.root, text="PRESS", command= self.amount_pressed)
self.button.pack()
self.label.pack()
self.mainloop = mainloop()
def amount_pressed(self):
self.changer.set(int(self.changer.get()) + 1)
test = Press()
Explanation:
By using self.changer.set(+1), you are setting self.changer to a positive one (as opposed to adding one to it).
What you want to do is:
get the string value of self.changer
convert it to an integer
add 1 to it
So:
self.changer.set(int(self.changer.get()) + 1)
This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 7 years ago.
class FCMenu:
def __init__(self,master):
frame=Frame(master)
frame.pack()
Label(frame, text="What is the number?").grid(row=0)
self.num = IntVar()
self.entry = Entry(frame,textvariable=self.num).grid(row=1)
button = Button(frame, text="Factorize", command=self.calc).grid(row=2)
self.resultvar = StringVar()
Label(frame, textvariable=self.resultvar).grid(row=3)
def calc(self):
e = int(self.entry.get())
print(e,self.num.get())
...
I am trying to create a Python GUI with tkinter, as shown above. However, every time I call .get() on the entry or the textvariable, it fails. With the entry itself, it explains that there is no .get() function for NoneType. If I remove that and use only self.num.get(), it prints 0 or 0.0, depending on whether or not I turn it to an integer. If I turn self.num to a StringVar, it prints nothing at all. Simply, I cannot find a way to obtain the input that I want to retrieve.
Made a simplified version of your code, and it prints correctly as the Entry widget is changed. Could be error elsewhere in your code? Or wrong indentation? Here is the code I tested with (this is 2.7 code, but worked with a 3.5 version of the code also):
import Tkinter as tk
class FCMenu:
def __init__(self, master):
frame = tk.Frame(master)
self.num = tk.IntVar()
self.entry = tk.Entry(frame, textvariable=self.num)
self.button = tk.Button(frame, text='Calc', command=self.calc)
frame.pack()
self.entry.pack()
self.button.pack()
def calc(self):
print(self.num.get(), self.entry.get())
root = tk.Tk()
frame = FCMenu(root)
root.mainloop()
It prints from both self.num.get() and self.entry.get(). The first is an Int and the second as String it seems.