CyberNinjas
CyberNinjas

Reputation: 1

When python code is executed, gui is empty

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()

Upvotes: 0

Views: 165

Answers (1)

abarnert
abarnert

Reputation: 366143

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.

Upvotes: 3

Related Questions