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.
Lindsay haisley
source share