I am trying to put a single vertical tkinter lable at the left of my window.
FONT = ('helvetica', 18)
write = 'hello world'
label = tk.Label(
window, anchor = 's', background = BACKGROUNDGREY, borderwidth = 0, font = FONT, foreground = 'white',
justify = 'center', pady = 40, relief = 'flat', text = write, width = len(write) * 2
)
label.pack(side = 'left', fill = 'y')
Everything works just fine as shown here:
But now, when I add this code to put a button...
btn = tk.Button(
label, activebackground = BACKGROUNDGREY, borderwidth = 0, background = BACKGROUNDGREY, height = 70,
highlightcolor = 'white', width = 70
)
btn.pack()
This happen:
as you can see, i don't know why but the label's text is not readable anymore and it also resize the width and I want to keep my label of the width shown in the first image.
UPDATE: I see that some of us did not understand: i want exatly the button inside the label.
You have put the button inside the label. Also, you've requested that the button be 70 characters tall. pack will shrink or expand a parent to fit its children, so the label is shrinking to exactly fit the button, and the button has a higher stacking order so it obscures the text of the label.
Related
I'm making a text editor and I'm adding a navbar but the text on FileBtn doesn't appear.
from tkinter import *
##############################################
# Constants
width = 800
height = 450
##############################################
# Window Creation
Window = Tk()
Window.geometry(F'{width}x{height}')
Window.title("TextWrite")
##############################################
# Navbar Creation
Navbar = Frame(Window, width = width, height = 25, bg = "#141414")
Navbar.place(x = 0, y = 0)
# File Options Creation
def OpenFileOptions():
FileOptions.place(x = 0, y = 25)
FileBtn = Button(Window, width = 10, height = 25, text = "File", bg = "#171717", fg = "white", command = OpenFileOptions)
FileBtn.place(x = 0, y = 0)
FileOptions = Frame(Window, width = 10, height = 50)
FileOptions.place(x = -1000, y = 0)
##############################################
# Text Input Creation
Input = Text(Window, width = width, height = 425, bg = "#202020", fg = "white")
Input.place(x = 0, y = 25)
Window.mainloop()
I searched for my problem but nothing I found seemed to fix it.
This is the first time I have encountered this error, and I have no clue why it happens.
The main problem is that you've set the height of the button to 25 lines tall. The width and height for some widgets -- including buttons -- is in number of characters, not pixels. Tkinter will center the text in the widget, so the text was very far down and out of view.
You can actually see this if you remove the text widget. You'll see that the button is very tall and the button label is centered in the button but roughly in the middle of the widget. (note: the following screenshot was taken on a Mac, which doesn't support changing the background color of buttons)
If you remove the height attribute altogether or set height to 1, you'll see the text, though you might have to also change the colors.
If you're just now learning tkinter, I strongly encourage you to use pack and/or grid rather than place. There's a slight learning curve, but it's much easier to make GUIs that are responsive and that make optimal use of the window size. place is best for very unique circumstances rather than as a general purpose tool.
but the text on FileBtn doesn't appear
No need to change value. In line 37, change this y=25 to y=400. Should be like this: Input.place(x = 0, y = 400)
Screenshot:
i have gui window with 3 frames and 3 labels.
the frames already change theyre size while resizing the window.
now i need to do the same with the fontsize of the labels.
i thought about using the tkinter event to resize the font.
the function for the event:
def leftoff(event):
fontsize = int(window_height/10)
return "break"
as i understand i have to bind that event to any object that i want to change, so thats my labelcode:
label1 = Label(top_frame, text="Top Frame",fg='#000000', bg = 'red')
label1.place(relx = 0.5, rely = 0.5, anchor = "center")
label1.bind("<ButtonRelease-1>",leftoff)
label1.config(font=("Courier bold", fontsize))
But the fontsize doesnt change. do i need some kind of update() for the root window ?
For my tkinter app I want to make a frame that would on top of other widgets, not taking any space (like html position fixed).
The Frame will have to contain widgets, if Frame is not possible labels or buttons will do.
I have no idea how to do it so haven't tried anything yet. Please help!
Here is a demonstration of place manager.
Remarks in the code explain behaviour.
import tkinter as tk
root = tk.Tk()
root.geometry("868x131")
button = tk.Button(root, text = "A Button")
button.place(x = 2, y = 2)
Frame = tk.LabelFrame(root, text = "A LabelFrame using place", bg="cyan")
# Frame using place with x, y, width, height in absolute coordinates
Frame.place(x = 250, y = 20, width = 600, height = 70)
ButtonInFrame = tk.Button(Frame, text = "A Button IN LabelFrame", bg="white")
# Note: place x,y is referenced to the container (Frame)
# Note: place uses just x,y and allows Button to determine width and height
ButtonInFrame.place(x = 2, y = 2)
Label = tk.Label(root, text = "A Label on LabelFrame\nWith multiple lines\nOf Text.", bg="light green")
# Label sits on top of buttonFrame and Frame
# Note place uses just x,y and allows Label to determine width and height
Label.place(x = 330, y = 60)
root.mainloop()
I want to align the second label(the blue text) under those two buttons(I aligned them on left side), I tried to use frames... or with grid but I still don't know how to do it..
Here is the image screenshot and here is the code:
from tkinter import *
root = Tk()
root.title("Tema 1")
root.geometry("550x600")
first_frame = Frame(root)
first_frame.pack(side=TOP)
first_label = Label(first_frame, text = "Exercitiul 1",font = "Times 15 bold")
button_ex1_1 = Button(first_frame, text = "Enunt", font = "Times 12 italic", command = lambda: btn_ex1_1(l1),height = 1, width = 20)
button_ex1_2 = Button(first_frame, text = "Rezolvare", font = "Times 12 italic", command = lambda: btn_ex1_2(l1),height = 1, width = 20)
l1 = Label(first_frame, font="Times 13 bold", fg="blue" , wraplength = 550, width=55)
first_label.pack(side=TOP)
button_ex1_1.pack(side=LEFT)
button_ex1_2.pack(side=LEFT)
l1.pack(side=BOTTOM)
The simplest solution is to pack the blue label before packing the buttons.
first_label.pack(side=TOP)
l1.pack(side=BOTTOM)
button_ex1_1.pack(side=LEFT)
button_ex1_2.pack(side=LEFT)
The packer works by aligning widgets to a side of existing empty space. When you put a widget on the left, the remaining empty space will be to the right of that widget. So, put the things on top first, followed by the things on the bottom, and then the things that go in the middle.
please help fix the code:
import Tkinter as tkinter
root = tkinter.Tk()
root.geometry("340x470")
row5 = tkinter.Frame(root)
row5.pack(side = 'top', fill = 'x')
label5 = tkinter.Label(row5, text = 'Audio', width = 10, anchor='e')
label5.pack(side = 'left')
entry5 = tkinter.Text(row5, height = '4')
entry5.pack(side = 'right', fill = 'x', expand = 'yes')
row6 = tkinter.Frame(root, borderwidth = 10, height = 20, bg = 'red')
row6.pack(side = 'top', fill = 'x')
button = tkinter.Button(row6, text = 'Send', height = 20, width = 20, relief = 'raised', cursor = 'hand1', font = ('times', 14, 'bold'))
button.pack()
root.mainloop()
the problem is that I have asked for "row6" height of 20, and the screen height is much greater than 20
The 'height' value in the button measures lines of text, not pixels. So you have requested a box tall enough to draw 20 lines of text. Try reducing it to 1 or 2:
button = tkinter.Button(row6, text = 'Send', height = 2, width = 20, relief = 'raised', cursor = 'hand1', font = ('times', 14, 'bold'))
There are two things working against you. First, when you specify the height of a button, if that button has text then the height attribute refers to lines of text. Thus, a height of 20 in your button requests a button 20 lines tall.
Second, is that the default behavior of a container widget (typically Frames, but actually any widget) is to "shrink to fit" or "expand to fit" everything it contains. So, even if you set the height of a frame to 20 pixels, it will actually be just big enough to contain all of its children. This is a Good Thing, and what you want 99.9% of the time. You should avoid trying to make fixed-size windows, and instead let tkinter decide the appropriate dimensions for you.
This feature of expanding or contracting is called "geometry propagation". You can turn this behavior off when you do need precise control of the size of a container widget. There are several examples on this website for how to do that. But again, it's rarely ever needed.