How make the tkinter widget ".place()" method in python work similarly to the ".setbounds()" method in Java? -
python 3.4 , eclipse ide
i have java form translating python tkinter. have number of jlabels, jtextfields, jcomboboxes, , seperator line need placed on identical python form respective python widget counterparts.
example label java using specific positioning:
jlabel lblwhichtrip = new jlabel("which trip:"); lblwhichtrip.sethorizontalalignment(swingconstants.right); lblwhichtrip.setfont(new font("tahoma", font.plain, 16)); lblwhichtrip.setbounds(64, 22, 135, 20); frmbookcalc.getcontentpane().add(lblwhichtrip);
after 50 odd browser tabs python equivalent should be:
self.lblwhichtrip = label(self, text = "which trip:", bg = "white", anchor = "e") self.lblwhichtrip.place(x = 64, y = 22, height=135, width=20)
but widget not appear @ all, not in wrong place.
the reason want use ".place()" method in python same reason used specific positioning in java, 1 has little precise control on ".pack()" method.
there instance found example of ".pack()" method being called directly before ".place()" method, more examples of ".place()" used.
so here question:
how utilize ".place()" method place python tkinter widgets on python form did java form? know close not working.
with more work have found button below put button on form labels section blank stated above.
languages = ['python','perl','c++','java','tcl/tk'] labels = range(5) in range(5): ct = [random.randrange(256) x in range(3)] brightness = int(round(0.299*ct[0] + 0.587*ct[1] + 0.114*ct[2])) ct_hex = "%02x%02x%02x" % tuple(ct) bg_colour = '#' + "".join(ct_hex) l = label(self, text=languages[i], fg='white' if brightness < 120 else 'black', bg=bg_colour) l.place(x = 20, y = 30 + i*30, width=120, height=25) self.quit = button(self) self.quit["text"] = "quit" self.quit["fg"] = "red" self.quit["command"] = self.quit self.quit.pack({"side": "left"})
the form being made in class structure denoted term "self". have no idea why button show on form labels not. difference ".pack()" , ".place()" methods used. research functionally same thing, go in different ways.
i have looked @ many examples eyes hurt. if clear forever thankful.
reference: reference tkinter
update:
the ".place()" not being inside class or don't know how instantiate inside class. got work neglecting class structure wanted use. how ".place()" work inside class?
update:
import tkinter tk import random root = tk.tk() w = 465 h = 590 x = (root.winfo_screenwidth()/2) - (w/2) y = (root.winfo_screenheight()/2) - (h/2) root.minsize(width=w, height=h) root.maxsize(width=w, height=h) root.resizable(width=tk.false, height=tk.false) root.geometry('%dx%d+%d+%d' % (w, h, x, y)) root.configure(background='white') root.option_add("*font", "tahoma 12") root.title("book calc") lblwhichtrip = tk.label(root, text = "which trip:", bg = "white", anchor = "e") lblwhichtrip.place(x=200, y=30, height=135, width=20) root.mainloop()
i ditched py file , class containing form stuff single file direct code , label not visible again. code above entirety of program @ moment. hope can figure out soon.
update:
the height , width needs on place , not label. above code modified.
lblwhichtrip = tk.label(root, text = "which trip:", bg = "white", anchor = "e") lblwhichtrip.place(x=200, y=30, height=135, width=20)
this works fine outside of class structure, doesn't seem work inside of class. half answer question above placing label or other widget in specific location on python form.
the other half question still unanswered being solution not work inside of class, should. if can shed light on this, great. else wanting write more robust program @ time better answer 1 needed.
thank you.
update:
root = tk.tk() app = application(root)
the above when create class. root needs sent directly.
def __init__(self, master): self.parent = master
a pointer root needs set class var persistence.
lblwhichtrip = tk.label(root, text = "which trip:", bg = "white", anchor = "e") lblwhichtrip.place(x=85, y=-35, height=135, width=100)
then can make widgets want inside class, , layout engines work fine. pack, place, , grid work perfectly.
answered due diligence.
refernce: additional info here
Comments
Post a Comment