I'm making a simple app just to practice python in which I want to write text as if it were Notepad. However, I can't make my entry bigger. I'm using tkinter for this. Does anybody know how to make the height of an entry bigger?
I tried something like this:
f = Frame()
f.pack()
e = Entry(f,textvariable=1,height=20)
e.pack()
I know this doesn't work because there isn't a property of "height". However, I see that there is a width property.
It sounds like you are looking for tkinter.Text, which allows you to adjust both the height and width of the widget. Below is a simple script to demonstrate:
from tkinter import Text, Tk
r = Tk()
r.geometry("400x400")
t = Text(r, height=20, width=40)
t.pack()
r.mainloop()
Another way would be to increase the internal padding by adding this in the pack method:
...
e = Entry(f,textvariable=1,height=20)
e.pack(ipady=3)
...
for instance. This worked for me for an 'Entry' and it also works with .grid()
Actually it's very easy. You don't need to set height in Entry(), but in place().
for example:
from tkinter import Entry, Tk
window = Tk()
t = Entry(window)
t.place(width=150,height=50)
window.mainloop()
from tkinter import *
root=Tk()
url = Label(root,text="Enter Url")
url.grid(row=0,padx=10,pady=10)
entry_url = Entry(root,width="50")
entry_url.grid(row=0,column=1,padx=5,pady=10,ipady=3)
root.geometry("600x300+150+150")
root.mainloop()
learn more follow this github
output image this is output of above code
You can also change it by changing the font size :
Entry(
root,
font=("default", 40 or 20 whatever )
)
To change an entry widget's size, you have to change it's font to a larger font.
Here is my code:
import tkinter as tk
large_font = ('Verdana',30)
small_font = ('Verdana',10)
root = tk.Tk()
entry1Var = tk.StringVar(value='Large Font!')
entry1 = tk.Entry(root,textvariable=entry1Var,font=large_font)
entry1.pack()
entry2Var = tk.StringVar(value='Small Font!')
entry2 = tk.Entry(root,textvariable=entry2Var,font=small_font)
entry2.pack()
root.mainloop()
You can change the height of the entry widget.
To do so you can write:
entry_box.place(height=40, width=100)
Change the value according to your needs!
IT WORKS !
By using the .place(width= , height= ) method, you can adjust the size of the entry. Another Method is to change the font of the text, but that limits your ability to change the font.
.place() method:
textbox.place(width= (Your desired width) ,height= (Your desired height))
Font Method:
textbox = Entry(root, font=("default", (Your font size))
Hope this helps!
Related
I am new to python and I have been learning tkinter recently. So I thought with myself that using the grid_forget() function I can remove a widget and redefine it. I thought of this animation that changes the padding of a label so it would create space (kind of like moving the label but not exactly). However, the animation does not work at all. The program freezes until the label reaches the last value of the padding. How can I fix this? Or is there a better way to animate a label moving in the screen?
Here is my code:
from tkinter import *
import time
root = Tk()
lbl = Label(root, text='------')
lbl.grid(row=0, column=0)
def animation():
padding = 0
while padding < 31:
lbl.grid_forget()
padding += 1
lbl.grid(row=0, column=0, padx=padding)
time.sleep(0.2)
# alternative: root.after(200, lambda: lbl.grid(row=0, column=0, padx=padding))
btn = Button(root, text='Animate', command=animation)
btn.grid(row=1, column=1)
root.mainloop()
You need to update the screen for changes to be shown.
Here is a working version using the .update() method:
from tkinter import *
import time
root = Tk()
lbl = Label(root, text='------')
lbl.grid(row=0, column=0)
def animation():
padding = 0
while padding < 31:
lbl.grid_forget()
padding += 1
lbl.grid(row=0, column=0, padx=padding)
root.update()
time.sleep(0.2)
# alternative: root.after(200, lambda: lbl.grid(row=0, column=0, padx=padding))
btn = Button(root, text='Animate', command=animation)
btn.grid(row=1, column=1)
root.mainloop()
Here is a way I also use to animate stuff on the screen, I am not able to understand what you were trying to achieve with your code snippet above, I tried making some changes to it but I feel this way is much better and let's you get more control of your window.
This uses the widely used Canvas widget in the tkinter library.
The Canvas is a general purpose widget, You can use it for a lot of things. Visit the hyper link for more clarity
Here is a short example of how you would create text on the screen.
from tkinter import *
root = Tk()
root.title("My animation")
c = Canvas(root)
x = 20
y = 20 #Instead of using row and column, you simply use x and y co-ordinates
#We will use these co-ordinates to show where the text is in the starting
my_text = c.create_text(x,y,text = '-----')
c.pack()
# This is all you need to create this text on your screen!
root.mainloop()
The idea is that you put your canvas up on your window , and then place whatever you want on it.
There are a lot more attributes that you can add to make your text look even better. Here is an in-depth tutorial on it.
Now that we have made your text widget, It is now time to move it around. Let us move it to 90,20 From our initial position which is 20,20
Here is how we will do it. If we simply move to text object to 90,90, We won't see any animations, it will just directly have it there. So what we will do is first create it at 21,20. Then 22,20. And so on...
We do this really fast till we reach 90,20
This looks like we are moving the text
from tkinter import *
import time
root = Tk()
root.title("My animation")
c = Canvas(root)
x = 20
y = 20 #Instead of using row and column, you simply use x and y co-ordinates
#We will use these co-ordinates to show where the text is in the starting
my_text = c.create_text(x,y,text = 'weee')
c.pack()
def animation():
y = 0.1
x = 0
for _ in range(1000):
c.move(my_text,x,y)
root.update()
anlabel = Button(root,text = 'Animate!',command = animation).pack()
root.mainloop()
This is not only applicable to text, but everything (like other images)that is there on the canvas. The canvas also has Events which will let you use mouse-clicks and other keys on the computer too.
I have made some changes from the previous code, But it is executable and you can try it for yourself to see how it works. increasing the value in time.sleep() makes the animation slower, the lesser the value, the faster.
Are you sure you aren't trying to do something more like the below example? Animating the padding on one of your widgets is going to screw up the rest of your display.
from tkinter import *
import time
root = Tk()
lbl = Label(root, text='')
lbl.grid(row=0, column=0)
def animation(step=12):
step = 12 if step < 0 else step
lbl['text'] = ' ------ '[step:step+6]
root.after(200, lambda: animation(step-1))
Button(root, text='Animate', command=animation).grid(row=1, column=0, sticky='w')
root.mainloop()
I want to update the label of a Tkinter LabelFrame widget.
For a Label widget, this can be done using the textvariable attribute, to which one can assign a StringVar.
I want to do the same thing, but for a LabelFrame
self.labelText = StringVar()
self.selectionFrame = ttk.LabelFrame(self, textvariable=self.labelText)
(...)
if A:
self.labelText.set("LabelA")
elif B:
self.labelText.set("LabelB")
How can I achieve this?
You can't. Neither the Tkinter LabelFrame nor the ttk LabelFrame support associating a variable with the widget.
If what you're really asking is how can you change the label, then you can use the configure method:
self.selectionFrame.configure(text="hello")
I just found some kind of a solution - using the labelwidget attribute to supply a separate Label object which uses the underlying StringVar:
self.labelText = StringVar()
self.labelWidget = Label(self, textvariable=self.labelText)
self.selectionFrame = ttk.LabelFrame(self, labelwidget=self.labelWidget)
This way, I can update the labelText to change the label of the LabelFrame
self.labelText.set("New Label")
I found that there is a problem to set a new label text due to text length.
So, I recommend that define a width of labelwidget as mentioned below:
self.labelWidget = Label(self, textvariable=self.labelText, width = 20)
Question
I'm trying to make a 'ttk Label' which a) is 20 pixels high by 300 pixels wide, b) is scrollable (in this case horizontally), and c) uses the simplest code possible within reason (except for the fact that the text and scrollbar are both within a frame*). I've found stackoverflow to be helpful in describing the processes I need to go through (put the label in a frame, put the frame in a canvas, put the scroll bar next to or underneath the canvas and 'bind' them together somehow), but despite looking at a fair few docs and stackoverflow questions, I can't figure out why my code isn't working properly. Please could someone a) update the code so that it satisfies the conditions above, and b) let me know if I've done anything unnecessary? Thanks
*the frame will be going in a project of mine, with text that is relevant
Current code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
myframe_outer = ttk.Frame(root)
mycanvas = tk.Canvas(myframe_outer, height=20, width=300)
myframe_inner = ttk.Frame(mycanvas)
myscroll = ttk.Scrollbar(myframe_outer, orient='horizontal', command=mycanvas.xview)
mycanvas.configure(xscrollcommand=myscroll.set)
myframe_outer.grid()
mycanvas.grid(row=1, sticky='nesw')
myscroll.grid(row=2, sticky='ew')
mycanvas.create_window(0, 0, window=myframe_inner, anchor='nw')
ttk.Label(myframe_inner, text='test ' * 30).grid(sticky='w')
root.mainloop()
Edit:
Current result
Answer
Use a readonly 'entry' widget - it looks the same as a label, and doesn't need to be put in a canvas.
Code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
mytext = tk.StringVar(value='test ' * 30)
myframe = ttk.Frame(root)
myentry = ttk.Entry(myframe, textvariable=mytext, state='readonly')
myscroll = ttk.Scrollbar(myframe, orient='horizontal', command=myentry.xview)
myentry.config(xscrollcommand=myscroll.set)
myframe.grid()
myentry.grid(row=1, sticky='ew')
myscroll.grid(row=2, sticky='ew')
root.mainloop()
Result
I'm new to tkinter and I have this code in python:
#import the tkinter module
from tkinter import *
import tkinter
calc_window = tkinter.Tk()
calc_window.title('Calculator Program')
button_1 = tkinter.Button(text = '1', width = '30', height = '20')
button_1 = '1'
calc_window.mainloop()
But when I run it, the button doesn't appear. Does anyone know why? Thank you!
Getting a widget to appear requires two steps: you must create the widget, and you must add it to a layout. That means you need to use one of the geometry managers pack, place or grid to position it somewhere in its container.
For example, here is one way to get your code to work:
button_1 = tkinter.Button(text = '1', width = '30', height = '20')
button_1.pack(side="top")
The choice of grid or pack is up to you. If you're laying things out in rows and columns, grid makes sense because you can specify rows and columns when you call grid. If you are aligning things left-to-right or top-to-bottom, pack is a little simpler and designed for such a purpose.
Note: place is rarely used because it is designed for precise control, which means you must manually calculate x and y coordinates, and widget widths and heights. It is tedious, and usually results in widgets that don't respond well to changes in the main window (such as when a user resizes). You also end up with code that is somewhat inflexible.
An important thing to know is that you can use both pack and grid together in the same program, but you cannot use both on different widgets that have the same parent.
from tkinter import *
import tkinter
calc_window = tkinter.Tk()
calc_window.title('Calculator Program')
frame = Frame(calc_window )
frame.pack()
button_1 = tkinter.Button(frame,text = '1', width = '30', height = '20')
button_1.pack(side=LEFT)
calc_window.mainloop()
try adding the button using pack(). i donno why u tried to assign button_1 = '1' in your code
a neat example:
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(
frame, text="QUIT", fg="red", command=frame.quit
)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
You're not packing the button_1. The code is:
from tkinter import *
root = Tk()
root.title('Calculator Program')
button_1 = Button(root, text='1', width='30', height='20')
button_1.pack()
root.mainloop()
It's simple!
Hope this helps!
from tkinter import *
calc_window = Tk()
calc_window.title('Calculator Program')
button_1 = Button(text = '1')
button_1.place(x=0,y=0,width = 30, height = 20)
calc_window.mainloop()
I'm trying to change the layering of Tkinter Canvas widgets. With most widgets you can force the widget above other widgets by using the lift method. However, if I try the same on a Canvas widget I get an error.
Error :
TypeError: tag_raise() got an unexpected keyword argument 'aboveThis'
An Example of my Problem :
import Tkinter as Tk
root = Tk.Tk()
w, h = 200, 200
a = Tk.Canvas(root, bg='red', width=w, height=h)
a.grid(column=0, row=0)
b = Tk.Canvas(root, bg='blue', width=w, height=h)
b.grid(column=0, row=0)
a.lift(aboveThis=None)
root.mainloop()
If I do the same thing with Frame widgets, it works.
Example:
import Tkinter as Tk
root = Tk.Tk()
w, h = 200, 200
a = Tk.Frame(root, bg='red', width=w, height=h)
a.grid(column=0, row=0)
b = Tk.Frame(root, bg='blue', width=w, height=h)
b.grid(column=0, row=0)
a.lift(aboveThis=None)
root.mainloop()
The canvas lift() method is an alias for tag_raise(), which is used to raise not the canvas itself but entities within the canvas.
I found this comment within the Tkinter.py source code:
# lower, tkraise/lift hide Misc.lower, Misc.tkraise/lift,
# so the preferred name for them is tag_lower, tag_raise
# (similar to tag_bind, and similar to the Text widget);
# unfortunately can't delete the old ones yet (maybe in 1.6)
If you replace a.lift(aboveThis=None) with Tk.Misc.lift(a, aboveThis=None) then the canvas widget is raised correctly.
I came to this question because I was actually wanting to implement the equivalent of a tk statement like
canvas-pathName raise tagOrId ?aboveThis?
in order to raise individual canvas items to a particular z-position. For those interested in the same thing, I'll just post my realization (after a little head-scratching) that this can be done in Python quite easily:
canvasObject.tag_raise(tagOrId, tagOrId2)
The second argument here just gets incorporated into the tk command line, and is then interpreted as an "aboveThis" value.