Tkinter/ttk frame size - python

I'm trying to set the frame size via theme_settings and it just doesn't work.
from Tkinter import *
from ttk import *
root = Tk()
style = Style()
style.theme_settings('default',{'TFrame':{'configure':{'width':100, 'height':100}}})
frame = Frame(root)
frame.pack()
root.mainloop()
But if I set it explicitly, then it works:
frame.configure(width=100, height=100)
Why?
ttk.version = "0.3.1"
Tkinter.version = "$Revision: 81008 $"
P.S. I need to set this size via .theme_settings() method, the question exactly about it.
UPD: I've checked the same behavior with Button element and it works. Something is wrong with frames...
style = Style()
style.theme_settings('default',{'TButton':{'configure':{'width':100}}})
button = Button(root)
button.pack()
UPD2: The same story with 'padding'. It works with buttons, but not with frames. While frames 'background' for example can be set via theme_settings

The Answer should be like that:
root = tk.Tk()
width x height + x_offset + y_offset
root.geometry("500x300+250+100")

Use the Place management as following codes:
root = tk.Tk()
root.geometry("500x300+250+100") # width x height + x_offset + y_offset

Related

Tkinter Frame not appearing

I wrote a short piece of code in tkinter in python to see if I could make a frame appear in my window. Here is the code below:
from tkinter import *
root = Tk()
root.title("Window")
root.state("zoomed")
root.config(bg="white")
winHeight = int(root.winfo_height())
winWidth = int(root.winfo_width())
controlFrame = Frame(root, bg="red")
controlFrame.pack()
root.mainloop()
I created one full-sized window with a background colour of white. The frame inside it is supposed to be red. However, when I run this code, I do not see any red. I am sure I packed it and everything.
I'd love to help you out on this one...
There's just a slight detail that you might not notice right now but the frame, in fact, is present in the window, but it's too small to see. By this I mean you haven't specified the height and width of the frame that you have placed in the window. Here's the fixed version:
from tkinter import *
root = Tk()
root.title("Window")
root.state("zoomed")
root.config(bg="white")
winHeight = int(root.winfo_height())
winWidth = int(root.winfo_width())
controlFrame = Frame(root, bg="red", height = 700, width = 700)
controlFrame.pack()
root.mainloop()
What this will do is just set the height and width of the frame to 700px, so you will get a square frame of red colour.
I hope this answer was satisfactory.
The answer is pretty simple, you don't have any other widget in your frame, it's empty for now, so its size is 0 pixel (or 1, I don't remember). That's why you don't see it in your window.

Why isn't the label animation working till the last value of the loop?

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

tkinter python entry height

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!

Python Tkinter button not appearing?

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

How to center a Tkinter widget?

I have Tkinter window with canvas and label with 200x200 picture on it.
I want label to be in the center of the window, regardless of the window size.
from Tkinter import *
import Image, ImageTk, ImageDraw
imgsize = (200,200)
canvas_bg = "#000000"
root = Tk()
## root.geometry("350x350")
panel = PanedWindow()
panel.pack(expand=0)
canvas = Canvas(panel, bg=canvas_bg)
blank_source = Image.new('RGBA',imgsize, "#ffffff")
blank = ImageTk.PhotoImage(blank_source)
label = Label(canvas, image=blank)
label.configure(image = blank)
canvas.pack( expand=0)
mainloop()
Is there any way to do it?
Use the place geometry manager. Here is a simple example :
from tkinter import *
wd = Tk()
wd.config(height=500, width=500)
can = Canvas(wd, bg = 'red', height=100, width=100)
can.place(relx=0.5, rely=0.5, anchor=CENTER)
Basically the options work as follows:
With anchor you specify which point of the widget you are referring to and with the two others you specify the location of that point. Just for example and to get a better understanding of it, let's say you'd be sure that the window is always 500*500 and the widget 100*100, then you could also write (it's stupid to write it that way but just for the sake of explanation) :
from tkinter import *
wd = Tk()
wd.config(height=500, width=500)
can = Canvas(wd, bg = 'red', height=100, width=100)
can.place(x=200, y=200, anchor=NW)
relx and rely give a position relative to the window (from 0 to 1) : 0,4*500 = 200
x and y give absolute positions : 200
anchor=NW makes the offset options refer to the upper left corner of the widget
You can find out more over here :
http://effbot.org/tkinterbook/place.htm
And over here :
http://www.tutorialspoint.com/python/tk_place.htm

Categories