Why does the text on my button not appear? - python

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:

Related

Is there a Python tkinter function that makes a drawing’s coords on a certain ratio on the canvas widget?

I’m a first timer at tkinter(python) and what I want to do is to make a line of text stay on the same coords ratio on the canvas. For example, I want a line of text to stay in the middle. Is there any tkinter text parameters that make it stay in a certain ratio without running a while loop? I want minimal time complexity.
Your GUI can have a function bound to the Canvas <Configure> event that fires whenever the Canvas changes size. A simple example below.
There is also a Canvas.scale method which will change the location and size of canvas objects. Text may move but will not change size.
import tkinter as tk
root = tk.Tk()
# Create a canvas that will change size with the root window.
canvas = tk.Canvas( root, width = 600, height = 400, borderwidth = 2,
relief = tk.SOLID )
canvas.grid( sticky = 'nsew', padx = 5, pady = 5 )
root.grid_columnconfigure( 0, weight = 1 )
root.grid_rowconfigure( 0, weight = 1 )
text = canvas.create_text( 50, 50, text = 'Test Text' )
def on_canvas_config( event ):
""" This is bound to the canvas <Configure> event.
It executes when the canvas changes size.
The event is an object that contains the new width and height.
"""
x = max( 25, event.width // 2 ) # x >= 25
y = max( 12, event.height // 8 ) # y >= 12
canvas.coords( text, x, y ) # Set the canvas coordinates
canvas.bind( '<Configure>', on_canvas_config )
root.mainloop()
The on_canvas_configure function can be written to amend the coords of any objects in the Canvas to meet your requirements. I've never tried to use the Canvas.scale method but this may be worth exploring if there are many objects to reposition on the canvas.

Python tkinter - place function not working with multiple widgets

When making a program for a game database, I want to have the main menu to have multiple buttons leading to multiple topics. The place function isn't working for me. Here is my code:
windowFU = tk.Tk()
windowFU.title("MHFU Database")
windowFU.geometry("255x200+300+180")
frame = tk.Frame(master = windowFU, width = 255, height = 200)
frame.pack()
icon = tk.PhotoImage(file = "images/icon.png")
windowFU.iconphoto(False, icon)
welcome = tk.Label(
master = frame,
text = "What would you like to view?",
width = 30,
height = 2
)
searchEntry = tk.Entry(
master = frame,
width = 30
)
buttonMonstersFU = tk.Button(
master = frame,
text = "Monsters",
width = 12,
height = 2
)
# Here is the place function
buttonMonstersFU.place(x = 100, y = 100)
welcome.pack()
searchEntry.pack()
buttonMonstersFU.pack()
searchEntry.bind('<Return>', getEntry)
windowFU.mainloop()
Note: Currently I just have the place function set to x = 100, y = 100 to test, it is not my final location.
Here is an image of what I get:
Result
What should I do?
You call buttonMonstersFU.pack() a few lines after you call buttonMonsersFU.place(...). Only one of pack, place, or grid can be responsible for a widget. When you call pack, any work done by place will be thrown away.
If you want to use place, then remove the line that calls buttonMonstersFU.pack().

Tkinter Label resize when packing a button

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.

Python canvas text position changes with font

I'm fairly new to Python and GUI programming, but while trying to create a simple canvas program I ran into the following problem:
When I use the create_text function(x, y etc.) it creates the text nicely at (x, y), but when I add a font the text shifts to the left and is no longer created at (x, y).
Can somebody tell me how I can fix this, and get the text at (60, 40) (see code)
from tkinter import *
class CanvasShapes:
def __init___(self):
master = Tk()
self.c1 = Canvas(master, width = 200, height = 100)
self.c1.grid(columnspan = 5)
#there are more buttons, but this is the only relevant one for this problem
Button(master, text = "String", command = self.String).grid(row = 1, column = 3)
def String(self):
self.c1.create_text(60, 40, text = "Hi, I am a string", font = "Times 16 bold underline")
def main():
Canvas1 = CanvasShapes()
main()
By default, the x,y represents the center point of the text. If you make the font larger or smaller, the center remains the same but the edges will change depending on the size of the text.
Add anchor='nw' to the create_text command to have the coordinates reoresent the upper-left (northwest) corner of the text.

how to set the exact height of an element in Tkinter?

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.

Categories