Specify the size of the Tkinter text field in pixels

How can I specify the size of a Tkinter text field using pixels? I am not trying to resize the font, I use this to help me scale it to the size of the window.

+12
python tkinter
source share
6 answers

You can do this by placing it inside the frame, forcing the frame to be fixed in size, disabling the size distribution, and adjusting the record to keep the borders of the frame. You should also work with the package too.

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() 
+7
source share

I had the same question, and it seems that the simplest answer is to create an instance of the Frame object of the specified size, pack it, and then use the place method to place and size the widgets inside it. I found that calling pack_propagate (0) on a Frame object is optional.

Here is a short piece of code that illustrates this method for several widgets.

The last widget created in this script is a text box widget with the size specified in pixels, according to 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, of course, blocks you in the place method for designing your Tkinter UI, but it allows you to specify the size of many widgets in pixels. Subsequent use of the pack method on child widgets will overwrite your placements and sizes.

Your code is likely to be better organized if you express it in an object-oriented manner, but this example just shows the sequence of operations needed to calibrate widgets in pixels, not other units.

+3
source share

This helped me a lot:

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

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

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

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

+1
source share

So often I 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 sizes up to 500 pixels in each direction. When you instantiate this class, it displays a window of this size. Hope this helps!

0
source share

Here is how I did it. I put the dimensions of the Text object in the placement tag

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

Height can be set using configure

 var = tk.Text(self) var.configure(height=1) 
0
source share

All Articles