当按钮尺寸更改时,按钮小部件的文本不显示

时间:2018-10-02 13:52:55

标签: python button text tkinter widget

我想创建具有特定尺寸的按钮,并在其中包含文本。 大多数较小的按钮都不会显示文本,无论所用文本的大小如何。

请参见以下示例:

from tkinter import *
main = Tk()
frame = Frame(main, width = 200, height = 40)
frame.pack()
button_list = []
for i in range(5):
    button_list.append(Button(frame, height = 40, width = 40, text = str(i)))
    button_list[i].place(x = i * 40, y = 0)

3 个答案:

答案 0 :(得分:2)

当您在按钮上使用文本而不是图像时,按钮width参数以字符数表示,而不是像素数。

您正在创建40个字符宽的按钮。但是,您将它们隔开40个像素。由于默认情况下,tkinter会将文本置于按钮的中心,因此每个按钮都会掩盖按钮之前的文本。

答案 1 :(得分:1)

作为Bryan Oakley答案的补充,您可以在package com.example.integrationamqp; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Bean; import org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter; import org.springframework.integration.amqp.inbound.AmqpInboundGateway; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessagingException; @SpringBootApplication public class IntegrationAmqpApplication { public static void main(String[] args) { new SpringApplicationBuilder(IntegrationAmqpApplication.class) .web(WebApplicationType.NONE) .run(args); } @Bean public MessageChannel amqpInputChannel() { return new DirectChannel(); } @Bean public AmqpInboundChannelAdapter inbound(SimpleMessageListenerContainer listenerContainer, @Qualifier("amqpInputChannel") MessageChannel channel) { AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(listenerContainer); adapter.setOutputChannel(channel); return adapter; } @Bean public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setConcurrentConsumers(2); // ... return container; } @Bean @ServiceActivator(inputChannel = "amqpInputChannel") public MessageHandler handler() { return new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { System.out.println(message.getPayload()); } }; } } 内指定按钮的宽度/高度(以像素为单位):

place

答案 2 :(得分:0)

根据this教程,在Button中为文本按钮指定了text lines的高度和宽度,而在pixels中指定了框架的尺寸。因此,您应该将按钮定义更改为:

for i in range(5):
    button_list.append(Button(frame, height = 1, width = 5, text = str(i)))        
    button_list[i].place(x = i * 40, y = 0)

这将产生以下输出:

enter image description here