0

whenever i execute this code, nothing shows up on the gui. it works fine if i use grid for placing labels and buttons. it doesnt show anything if i use .place to place the labels.

from Tkinter import *


class Applet(Frame):
""" First attempt to make the program """
    def __init__(self, master):
            """ initialize the frame """
            Frame.__init__(self,master)
            self.login()
    #self.Signup()
    def login(self):
            self.Login_username = StringVar()
            self.Login_password = StringVar()
            self.Label1 = Label(self, text = 'Username: ').place(x = 0, y = 0)
            self.Label2 = Label(self, text = 'Password: ').place(x =50, y = 0)
            self.loguser = Entry(self, textvariable = self.Login_username, width = 15).place(x = 0, y = 10)
            self.logpass = Entry(self, textvariable = self.Login_password, width = 15, show = '*').place(x = 50, y = 10)
            self.button = Button(self, text = 'Login').place(x = 400, y = 0)



Top = Tk()
Top.title('test-gui')
app = Applet(Top)
Top.geometry('700x350')
Top.mainloop()
3
  • Don't know TKinter but what is the return value of .place()? If it returns the instance great, otherwise that might become an additional problem down the road. Commented Apr 17, 2013 at 19:38
  • @David: Like most mutating methods in idiomatic Python, it returns None, so yes, it is an additional problem down the road. As was already explained in the answer. Commented Apr 17, 2013 at 19:49
  • @abarnert I wrote that comment before you posted your question but got a phone call, when I got back I posted... hence missing your answer. Commented Apr 17, 2013 at 20:26

1 Answer 1

3

You're just creating a bunch of objects and adding them to an interface that isn't itself added anywhere.

The simplest way to add them to the interface is to just call the pack method on the Applet.

However, you're still going to have some problems.

First, you're trying to explicitly place all your elements almost on top of each other, so they're all going to overlap in a big mess.

Second, the place method returns None, so all of your member variables are going to be None, not the actual widgets.

Here's a version that solves all three problems:

from Tkinter import *

class Applet(Frame):
    """ First attempt to make the program """
    def __init__(self, master):
            """ initialize the frame """
            Frame.__init__(self,master)
            self.login()
    #self.Signup()
    def login(self):
            self.Login_username = StringVar()
            self.Login_password = StringVar()
            self.Label1 = Label(self, text = 'Username: ')
            self.Label1.place(x = 0, y = 0)
            self.Label2 = Label(self, text = 'Password: ')
            self.Label2.place(x = 100, y = 0)
            self.loguser = Entry(self, textvariable = self.Login_username, width = 15)
            self.loguser.place(x = 0, y = 20)
            self.logpass = Entry(self, textvariable = self.Login_password, width = 15, show = '*')
            self.logpass.place(x = 100, y = 20)
            self.button = Button(self, text = 'Login')
            self.button.place(x = 400, y = 20)

Top = Tk()
Top.title('test-gui')
app = Applet(Top)
app.pack(fill='both', expand=True)
Top.geometry('700x350')
Top.mainloop()

However, you're usually better off using boxes and the pack method instead of explicitly calling place. For example, the x = 100 instead of x = 50, etc., works on my system, making everything lay out nicely—but if your system has different default font sizes, widget boundaries, etc., it will end up overlapping or weirdly spaced.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.