Python 以像素为单位指定 Tkinter 文本框的尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14887610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:45:59  来源:igfitidea点击:

Specify the dimensions of a Tkinter text box in pixels

pythontkinter

提问by xxmbabanexx

How would I specify the dimensions of a Tkinter text box using pixels? I am not trying to change the font size, I am using this to help me scale it to the size of the window.

如何使用像素指定 Tkinter 文本框的尺寸?我不是要更改字体大小,而是使用它来帮助我将其缩放到窗口的大小。

回答by erdekhayser

This is the way I often do this:

这是我经常这样做的方式:

from tkinter import *

class App:
    def __init__(self):
        self.tk = Tk()
        self.canvas = Canvas(self.tk, width = 500, height = 500)
        self.canvas.pack()

This sets the dimensions to 500 pixels in each direction. When you create an instance of this class, it displays a window of this size. Hope this helps!

这将每个方向的尺寸设置为 500 像素。创建此类的实例时,它会显示此大小的窗口。希望这可以帮助!

回答by Gonzo

You can do it by putting it inside a frame, forcing the frame to a fixed size by deactivating size propagation and configuring the entry to stick to the frame borders. Should work in a similar way with pack, too.

您可以通过将其放入框架中来实现,通过停用大小传播并将条目配置为坚持框架边界来强制框架为固定大小。也应该与 pack 以类似的方式工作。

import Tkinter  # tkinter with small t for python 3
#import ttk  # nicer widgets

root = Tkinter.Tk()

mainFrame = Tkinter.Frame(root)
mainFrame.grid()
button = Tkinter.Button(mainFrame, text="dummy")
button.grid()


entryFrame = Tkinter.Frame(mainFrame, width=454, height=20)
entryFrame.grid(row=0, column=1)

# allow the column inside the entryFrame to grow    
entryFrame.columnconfigure(0, weight=10)  

# By default the frame will shrink to whatever is inside of it and 
# ignore width & height. We change that:
entryFrame.grid_propagate(False)
# as far as I know you can not set this for x / y separately so you
# have to choose a proper height for the frame or do something more sophisticated

# input entry
inValue = Tkinter.StringVar()
inValueEntry = Tkinter.Entry(entryFrame, textvariable=inValue)
inValueEntry.grid(sticky="we")


root.mainloop()

回答by decadenza

This helped me a lot:

这对我帮助很大:

You can also use the height and width options to explicitly set the size. If you display text in the button, these options define the size of the button in text units. If you display bitmaps or images instead, they define the size in pixels (or other screen units). You can specify the size in pixels even for text buttons, but that requires some magic. Here's one way to do it (there are others):

f = Frame(master, height=32, width=32) f.pack_propagate(0) # don't shrink f.pack()

b = Button(f, text="Sure!") b.pack(fill=BOTH, expand=1)

您还可以使用高度和宽度选项来明确设置大小。如果在按钮中显示文本,这些选项以文本单位定义按钮的大小。如果您改为显示位图或图像,则它们以像素(或其他屏幕单位)为单位定义大小。您甚至可以为文本按钮指定像素大小,但这需要一些魔法。这是一种方法(还有其他方法):

f = Frame(master, height=32, width=32) f.pack_propagate(0) # 不要收缩 f.pack()

b = Button(f, text="Sure!") b.pack(fill=BOTH, expand=1)

Source: http://effbot.org/tkinterbook/button.htm

资料来源:http: //effbot.org/tkinterbook/button.htm

回答by Lindsay Haisley

I've had the same question, and it seems that the simplest answer is to instantiate a Frame object of a specified size, pack it, and then use the place method to place and size widgets within it. I've found that invocation of the pack_propagate(0) method on the Frame object is unnecessary.

我有同样的问题,似乎最简单的答案是实例化指定大小的 Frame 对象,将其打包,然后使用 place 方法在其中放置和调整小部件的大小。我发现在 Frame 对象上调用 pack_propagate(0) 方法是不必要的。

Here's a short piece of code which exemplifies this method on several widgets.

这是一小段代码,它在几个小部件上举例说明了这种方法。

The last widget instantiated in this script is a text box widget with the size specified in pixels, as per your question.

根据您的问题,此脚本中实例化的最后一个小部件是一个文本框小部件,其大小以像素为单位。

from Tkinter import *

root = Tk()
root.title("Fee Fie Foe Fum")
frame=Frame(root, width=300, height=160)
frame.pack()
button1 = Button(frame, text="Mercy!")
button1.place(x=10, y=10, height=30, width=100)
button2 = Button(frame, text="Justice!")
button2.place(x=10, y=50, height=30, width=100)
text1 = Label(text="Verdict:")
text1.place(x=10, y=90)
tbox1 = Text(frame)
tbox1.place(x=10, y=115, height=30, width=200)

root.mainloop()

This does, of course, lock you into the place method for designing your Tkinter UI, but it does allow you to specify the dimensions of many widgets in pixels. Subsequent use of the pack method on child widgets will overwrite your placements and sizing.

当然,这确实会将您锁定在用于设计 Tkinter UI 的 place 方法中,但它确实允许您以像素为单位指定许多小部件的尺寸。随后在子小部件上使用 pack 方法将覆盖您的展示位置和大小。

Your code will probably be better organized if you express it in an object oriented fashion, but this example simply shows the sequence of operations necessary to accomplish sizing of widgets in pixels rather than other units.

如果您以面向对象的方式表达它,您的代码可能会被更好地组织起来,但此示例仅显示了以像素而不是其他单位完成小部件大小调整所需的操作序列。

回答by xwaxes

Here is how I did it. I placed the dimensions of the Text object inside the place tag

这是我如何做到的。我将 Text 对象的尺寸放置在 place 标签内

    firstname = Text()
    firstname.place(x = 170, y = 20, height = 25, width = 200)

回答by Richard Morris

The height can be set using configure

可以使用configure设置高度

var = tk.Text(self)
var.configure(height=1)

回答by Ish Beniwal

entry_2 = Entry(win, justify = 'center')
entry_2.grid(row =2,column = 7,ipadx=10, ipady=10)