Python Tkinter Label in Frame - python

I want to place a label inside a frame in tkinter, but I can't figure out how to actually get it inside.
import tkinter
from tkinter import *
W=tkinter.Tk()
W.geometry("800x850+0+0")
W.configure(background="lightblue")
FRAME=Frame(W, width=100, height =50).place(x=700,y=0)
LABEL=Label(FRAME, text="test").pack()
When I run this, it doesn't place the Label inside the frame, but just places it normally on the window.
What am I doing wrong?

In the line
FRAME=Frame(W, width=100, height =50).place(x=700,y=0)
You think you are returning a tk frame, but you are not! You get the return value of the place method, which is None
So try
frame = Frame(W, width=100, height=50)
frame.place(x=700, y=0)
label = Label(frame, text="test").pack()
If you don't want the frame to shrink to fit the label, use (How to stop Tkinter Frame from shrinking to fit its contents?)
frame.pack_propagate(False)
Note: Either import tkinter or from tkinter import * but not both. Also, by convention, names of instances of objects are lowercase.

I think it's because you're assigning FRAME to Frame(W, width=100, height =50).place(x=700,y=0), as opposed to just the actual frame, and according to the Place Manager reference, there doesn't seem to be a return value. Try this:
import tkinter
from tkinter import *
W=tkinter.Tk()
W.geometry("800x850+0+0")
W.configure(background="lightblue")
FRAME=Frame(W, width=100, height =50)
FRAME.place(x=700,y=0)
LABEL=Label(FRAME, text="test").pack()
W.mainloop()

Related

what is the difference between Label and ttk.Label in tkinter

I have a code but After add just Label frame not working fine like that :
label = Label(self, text="Test-1")
label .grid(row=1, stick="w", padx=20)
The Label not go in Left.
but after add ttk :
label = ttk.Label(self, text="Test-1")
label .grid(row=1, stick="w", padx=20)
the Label Frame work fine why?
Because in the first one you didn't use this phrase
from tkinter import *
The * imports everything related to tkinter but
if you used this
from tkinter import ttk
you only import the function that create the table only

My Tkinter photo is packed, but it shows as a blank

I can't seem to get the picture to show up on screen while having a toplevel frame on top of my main one (root). This one is just called "frame". I have circled the outline of the tkinter Frame on the included photo in this post. When I resize the picture, the green frame outline changes, but the picture itself won't show up.
I also tried to pack it on my main root window, with success, which suggests it's a toplevel window issue. I just don't know what it is. Any ideas?
Here is my code:
def show_topLevelWindow():
from tkinter import ttk
print("Entered to show results")
window_linkedin = Toplevel(root)
window_linkedin.geometry('1000x590')
frame = Frame(window_linkedin)
frame.pack()
error_frame = tkinter.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
error_label = Label(frame, text="It appears there are no results for the selected country")
error_label.config(font=("Helvetica Neue", 20))
im_error = Image.open("./ressources/images_gui/aw_snap.png")
im_error = im_error.resize((500, 500), Image.ANTIALIAS)
im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
im_error_label = Label(frame, image=im_error)
try:
if ....:
....unimportant code ....
else:
error_label.pack(in_=error_frame)
im_error_label.pack(in_=error_frame)
error_frame.pack(anchor="center")
except Exception as e:
error_label.pack(in_=error_frame)
im_error_label.pack(in_=error_frame)
error_frame.pack(anchor="center")
Packed image shows as blank
The single most important issue you are having is your image is not being saved for reference. if you add global im_error to the very top of your function your image will be visible.
That said there are some issues with your code you should correct.
First: Do not import in a function. Instead write all your imports at the top of your code.
Second: I am not sure why you are doing .pack(in_=error_frame). This is not something one would ever really need. Just make sure that your label is already assigned to the correct frame. The in_ argument is rarely used and probably most people never use it. I have been on here for two years now and this is the first time I have seen anyone use that argument.
Third: You have not shown your imports for Tkinter however based on how you have written you code it looks like you have done:
import tkinter
from tkinter import *
This is overkill and is not a good idea. Just do import tkinter as tk and make sure you use tk. prefix where it applies.
Here is your code remade:
import tkinter.ttk as ttk
import tkinter as tk
from PIL import ImageTk, Image
def show_toplevel_window():
global im_error
window_linkedin = tk.Toplevel(root)
window_linkedin.geometry('1000x590')
frame = tk.Frame(window_linkedin)
frame.pack()
error_frame = tk.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
error_frame.pack()
error_label = tk.Label(frame, font=("Helvetica Neue", 20), text="It appears there are no results for the selected country")
error_label.pack()
im_error = Image.open("./ressources/images_gui/aw_snap.png")
im_error = im_error.resize((500, 500), Image.ANTIALIAS)
im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
im_error_label = tk.Label(error_frame, image=im_error)
im_error_label.pack()
root = tk.Tk()
show_toplevel_window()
root.mainloop()

How to use tkinter.place()?

I want to have an entry and I want to have a listbox of fixed size under it which is fixed. and I want to have another listbox of dynamic height. That will appear and disappear in time and also change in size. I want the second listbox (which is actually a dropdown) to be shown over the other listbox which I want it to be fixed. My code for changing the size etc is correct and works perfectly with pack() but then it will move the other listbox up and down as it's size changes. And when I change pack() to place(...) it's not shown at all anymore.
Here is my code:
from tkinter import *
root = Tk()
entry = Entry(
root,
width=50
)
frame = Frame(
root,
height=10,
width=50,
background="#caeaa9"
)
dropdown = Listbox(
frame,
background="#11FF11",
height=5,
width=50
)
listbox = Listbox(
frame,
background="#FF1111",
height=10,
width=50
)
entry.pack()
listbox.pack()
dropdown.place()
frame.pack()
mainloop()
But the dropdown does not appear when I run it. What am I missing?
By the way, I want the top border of the dropdown to be exactly on the top border of the listbox and both of them right under the entry.
I highly recommend you don't use the place geometry manager. Ever. If you want to create larger or more complex interfaces, having to place widgets is terrible. I suggest using grid instead:
import tkinter as tk
# Avoid wildcard imports!
root = tk.Tk()
entry = tk.Entry(
root,
width=50
)
frame = tk.Frame(
root,
background="#caeaa9"
)
listbox = tk.Listbox(
frame,
background="#FF1111",
height=10,
width=50
)
dropdown = tk.Listbox(
frame,
background="#11FF11",
height=5,
width=50
)
entry.pack()
listbox.grid(column=0, row=0, sticky=N)
dropdown.grid(column=0, row=0, sticky=N)
frame.pack()
root.mainloop()
You have two problems:
you aren't telling place where to place the widget
the stacking order (z-index) of the dropdown is behind (lower) than the other listbox, so it will appear under the listbox.
give explicit coordinates to place
I'm not entirely clear where you want the dropdown to appear. Since you say "over" the other listbox, I suggest you use the in_ parameter to make the coordinates relative to the other listbox, and then use other place arguments to place it exactly where you want.
Example:
In the following example I make the dropdown exactly the same width, but half the height of the other listbox.
dropdown.place(in_=listbox, x=0, y=0, anchor="nw", relwidth=1.0, relheight=.5)
Fix the stacking order
All widgets have a stacking order. Some people call this a z-index. By default the order is the order in which the widgets are created. Since you create the dropdown before the other listbox, the other listbox has a higher stacking order. That means that it will appear above the dropdown.
A simple solution is to create the dropdown last. If you don't want to do that, you can call the lift method of the widget to raise its stacking order. The argument for lift is the name of a widget you want to be above.
Example:
dropdown.lift(listbox)
Tkinter place is one of the three geometry manager in tkinter. it allows us to specify the position of tkinter widgets in terms of x and y coordinate.
Here is the code example :
from tkinter import *
window = Tk()
Button(window,text = "Click Me").place(x = 50,y = 50)
window.mainloop()
For more info kindly refer this tutorial on tkinter place

tkinter: simple scrollable text

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

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!

Categories