-1

I have tried to align the following entry widget but it doesn't work. I was able to align it properly when I haven't put it into a frame but now it is not aligned properly. Before it was on the left side and the labels are next to the entry box but now it's at the top center. I tried to change the coordinates but it does not work. I want to be able to align it the same as before on the left side as I will put something else on the right side.

def userinput():

        #first
        frame=Frame(root)
        frame.place(x=55,y=50)
        frame.pack()
        L1=Label(frame,text="1",font=("calibri"))
        L1.place(x=55,y=50)
        L1.pack()

        frame1=Frame(root)
        frame1.place(x=70,y=50)
        frame1.pack()
        E1 = tk.Entry(frame1, width=30, bd=4)
        E1.pack()
        E1.bind('<Return>', lambda event, arg=(0): answer(event, arg))

        #second
        frame2=Frame(root)
        frame2.place(x=55,y=80)
        frame2.pack()
        L2 = Label(frame2,text="2",font=("calibri"))
        L2.pack()

        frame3=Frame(root)
        frame3.place(x=70,y=80)
        frame3.pack()
        E2 = tk.Entry(frame3,width=30, bd=4)
        E2.pack()
        E2.bind('<Return>', lambda event, arg=(1): answer(event, arg))

        #third
        frame4=Frame(root)
        frame4.place(x=55,y=110)
        frame4.pack()
        L3 = Label(frame4,text="3",font=("calibri"))
        L3.pack()

        frame5=Frame(root)
        frame5.place(x=70,y=110)
        frame5.pack()
        E3 = tk.Entry(frame5, width=30,bd=4)
        E3.pack()
        E3.bind('<Return>', lambda event, arg=(2): answer(event, arg))
Sirius
  • 27
  • 1
  • 12

1 Answers1

0

Is this what you're looking for?

class UI(Frame):
    def __init__(self, parent):
        self.parent = parent
        Frame.__init__(self, parent)
        self.create_widgets(parent)

    def create_widgets(self, root):
        #first
        f1 = Frame(root)
        e1 = Entry(f1, width=30, bd=4)
        l1 = Label(f1, text="1")
        e1.grid(row=0, column=0)
        l1.grid(row=0, column=1)
        f1.grid()
        #second
        f2 = Frame(root)
        e2 = Entry(f2, width=30, bd=4)
        l2 = Label(f2, text="2")
        e2.grid(row=0, column=0)
        l2.grid(row=0, column=1)
        f2.grid()
        #third
        self.s1 = IntVar()
        self.s1.set(3)
        f3 = Frame(root)
        e3 = Entry(f3, width=30, bd=4)
        l3 = Label(f3, textvariable=s1)
        e3.grid(row=0, column=0)
        l3.grid(row=0, column=1)
        f3.grid()

   def set_score(self, score):
       self.s1.set(score)

def test_ui():
    root = Tk()
    app = UI(root)
    app.set_score(4)
    app.mainloop()

test_ui()

I find gridding the widgets a lot more simpler than packing them.

rolika
  • 381
  • 1
  • 10
  • Hey i have a question i get this error: Traceback (most recent call last): File "C:\Users\name\Desktop\Portable Python 3.2.5.1\App\lib\tkinter\__init__.py", line 1456, in __call__ return self.func(*args) File "", line 248, in File "", line 260, in answer TypeError: unsupported operand type(s) for +=: 'IntVar' and 'int' when i tried to put this code: def scoreboard(): global score score = IntVar() scoreupdate = tk.Label(root, textvariable=score).pack() score.set(score.get()) scoreboard() to update score but it remains 0 – Sirius Apr 26 '17 at 06:08
  • @Arielle I think this should be a separate question. If score is global, why declaring as a new `IntVar()` instance (which defaults to 0), then assigning it to a Label (as a 0), then getting and setting it again (as a 0). Is it possible that the global score variable is used as an integer somewhere in your code, and if you redefine it in `scoreboard()`, it gets mixed up as now being an IntVar()? – rolika Apr 26 '17 at 07:00
  • yeah that's right. It sets it to 0, well I just want to show the update to the score but i don't know how to do that. – Sirius Apr 26 '17 at 07:44
  • @Arielle i edited my answer to give you an idea how i would do this. – rolika Apr 26 '17 at 07:46
  • ey thank you but I figured it out now. I understand now why I was getting errors, it's because intvar() gives me an object not an int. The thing is I can't ask questions for 5 days cause I got downvoted questions and I might need to ask questions later, idk if I will, do you have any contact details? – Sirius Apr 26 '17 at 08:49
  • @Arielle if the answer was helpful, please consider marking it as an accepted asnwer ;-) & feel free to ask in the comments i'll have a look at it and help if i can – rolika Apr 26 '17 at 09:09
  • It says here to avoid extended discussions in comments. Btw, yeah i accepted it as an answer. – Sirius Apr 26 '17 at 09:26
  • @Arielle Have a look at the chat, apparently it is used for problem solving too :-) [link](http://chat.stackoverflow.com/rooms/6/python) – rolika Apr 26 '17 at 09:49
  • I don't have enough reputation to go on chat. :/ – Sirius Apr 26 '17 at 09:54
  • @Arielle submit your script on codepad.org, and share the link here in the comments :-) – rolika Apr 27 '17 at 08:21
  • is it possible to do a drag and drop of text or label into a entry widget? – Sirius Apr 27 '17 at 19:28
  • @Arielle [tkdnd](http://stackoverflow.com/questions/16143460/drag-and-drop-in-tkinter) – rolika Apr 28 '17 at 06:18
  • have a look at this question if you want http://stackoverflow.com/questions/43705983/python-tkinter-how-to-display-an-array-of-images-in-a-loop – Sirius Apr 30 '17 at 12:51