Python 3.4 and Eclipse IDE
I have a java form I am translating into python tkinter. I have a number of jlabels, jtextfields, jcomboboxes, and a seperator line that need to be placed on an identical python form with their respective python widget counterparts.
Example label from 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 some odd browser tabs the 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 the widget does not appear at all, not even in the wrong place.
The reason I want to use the ".place()" method in python is the same reason I used specific positioning in java, one has very little precise control over the ".pack()" method.
There was an instance where I found an example of the ".pack()" method being called directly before the ".place()" method, but more examples of just the ".place()" only used.
So here is the question:
How do I utilize the ".place()" method to similarly place the python tkinter widgets on the python form as I did with the Java form? I know I am close but it is not working.
With more work I have found the button below will put a button on my form but the labels section is blank as stated above.
languages = ['Python','Perl','C++','Java','Tcl/Tk']
labels = range(5)
for i in range(5):
ct = [random.randrange(256) for 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 is being made in a class structure as denoted by the term "self". I have no idea why the button will show on the form but the labels will not. the only difference is ".pack()" and ".place()" methods used. From my research they are functionally the same thing, they just go about it in different ways.
I have looked at so many examples my eyes hurt. If someone could clear this up i would be forever thankful.
Reference: Reference for tkinter
Update:
The ".place()" does not like being inside a class or I don't know how to instantiate it inside a class. I got it to work by neglecting the class structure that I wanted to use. How do I get ".place()" to work inside a class?
Update:
import tkinter as 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 the py file and the class containing all the form stuff for a single file with direct code and now the label is not visible again. The code immediately above is the entirety of my program at the moment. I hope I can figure this out soon.
Update:
The height and width needs to be on place and not the label. Above code modified.