0

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
texasman1979
  • 473
  • 1
  • 6
  • 17
  • You mentioned `place()` and `pack()`... did you look into `grid()`? – TigerhawkT3 Jun 15 '15 at 02:08
  • I could not get it to work either. Please reevaluate the question as I added more to it. – texasman1979 Jun 15 '15 at 02:25
  • additional 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? – texasman1979 Jun 15 '15 at 03:07
  • ".place() does not like being inside a class" is completely false, and makes no sense. It doesn't matter at all if it's in a class or not. – Bryan Oakley Jun 15 '15 at 21:04
  • What is your actual question at this point? there's a bunch of questions and an bunch of updates. What is it you're really asking? – Bryan Oakley Jun 26 '15 at 19:02

1 Answers1

-1
lblWhichTrip = tk.Label(root, text = "Which Trip:", bg = "white", anchor = "e")
lblWhichTrip.place(x=200, y=30, height=135, width=20)

This works perfectly fine outside of a class structure, but it doesn't seem to work inside of a class. This is a half answer for my question above for placing a label or other widget in a specific location on a python form.

The other half the question is still unanswered being that this solution does not work inside of a class, which it should. If someone can shed some light on this, it would be great. Someone else will be wanting to write a much more robust program than I at this time so a better answer than this one is needed.

Thank you.

Update:

root = tk.Tk()
app = Application(root)

The above is when you create the class. Root needs to be sent directly.

def __init__(self, master):
   self.parent = master

A pointer to root needs to be set a class var for persistence.

lblWhichTrip = tk.Label(root, text = "Which Trip:", bg = "white", anchor = "e")
lblWhichTrip.place(x=85, y=-35, height=135, width=100)

Then you can make all the widgets you want from inside a class, and all the layout engines will work fine. Pack, Place, and Grid will work perfectly.

Answered fully from due diligence.

Refernce: Additional info here

Community
  • 1
  • 1
texasman1979
  • 473
  • 1
  • 6
  • 17