简体   繁体   English

为什么我的按钮上的文字没有出现?

[英]Why does the text on my button not appear?

I'm making a text editor and I'm adding a navbar but the text on FileBtn doesn't appear.我正在制作一个文本编辑器并添加一个导航栏,但 FileBtn 上的文本没有出现。

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.主要问题是您将按钮的高度设置为 25高。 The width and height for some widgets -- including buttons -- is in number of characters, not pixels.某些小部件(包括按钮)的widthheight以字符数而非像素数表示。 Tkinter will center the text in the widget, so the text was very far down and out of view. Tkinter 将使小部件中的文本居中,因此文本非常靠下并且看不见。

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.您会看到按钮非常高,按钮 label 位于按钮的中央,但大致位于小部件的中间。 (note: the following screenshot was taken on a Mac, which doesn't support changing the background color of buttons) (注意:以下截图是在Mac上截取的,不支持改变按钮的背景颜色)

没有文本小部件的屏幕截图

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.如果您完全删除height属性或将height设置为1 ,您将看到文本,但您可能还必须更改 colors。


If you're just now learning tkinter, I strongly encourage you to use pack and/or grid rather than place .如果您刚刚开始学习 tkinter,我强烈建议您使用pack和/或grid而不是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.学习曲线略有不同,但制作响应式 GUI 并充分利用 window 大小要容易得多。 place is best for very unique circumstances rather than as a general purpose tool. place最适合非常独特的情况,而不是作为通用工具。

but the text on FileBtn doesn't appear但是 FileBtn 上的文本没有出现

No need to change value.无需更改值。 In line 37, change this y=25 to y=400 .在第 37 行中,将此y=25更改为y=400 Should be like this: Input.place(x = 0, y = 400)应该是这样的: Input.place(x = 0, y = 400)

Screenshot:截屏:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM