1

How to create multi-lines in an entry widget in tkinter and use those inputs to create something? For example, I want a textbox widget to come up and ask the user:

How many squares do you want? (ex: 4x4, 5x5)
What color do you want them?

And with the users input, I would like to create that many x-amount of squares in that specific height/width and specify the colors etc. I am totally new to tkinter and I'm not really sure how to approach this.

I tried using this, but i'm not really sure how to add more lines and to use the values inputted.

import tkinter
from tkinter import *

class Squares:
    root = Tk()
    root.title('Random')
    x = Label(text='How many squares? (ex: 4x4, 5x3)').pack(side=TOP,padx=10,pady=10)
    Entry(root, width=10).pack(side=TOP,padx=10,pady=10)
    Button(root, text='OK').pack(side= LEFT)
    Button(root, text='CLOSE').pack(side= RIGHT)
3
  • That's not how you use classes in Python. I'm not sure what Squares is supposed to be doing, but I am sure that it's not going to do what it's supposed to. Commented Mar 9, 2013 at 2:04
  • Keep in mind that Label(...).pack() returns None, so you are not assigning the widget to the variable x. Commented Mar 9, 2013 at 2:19
  • 1
    Have you worked through An Introduction To Tkinter, or any other kind of tutorial? The second example shows how to create an application class, and how to make buttons that perform actions. The only piece it's missing is getting the value out of the Entry and doing something with it. Commented Mar 9, 2013 at 2:19

2 Answers 2

8

You have a number of problems here.

I'm not sure what the Squares class is supposed to be doing, but it's basically not doing anything. You have a bunch of code that runs when you define the class, creating a few variables (which will end up as class attributes, shared by all instances of the class), and… that's it. Rather than try to figure out what you're intending here, I'm just going to scrap the class and make it all module-level code.

You never call root.mainloop(), so your program will just define a GUI and then never run it.

You don't bind your buttons to anything, so there's no way they can have any effect. You need to create some kind of function that does something, then pass it as the command argument, or .bind it later.

You don't store references for any of your controls, so there's no way to access them later. If you want to get the value out of the entry, you need some way to refer to it. (The exception is your x variable, but that's going to be None, because you're setting it to the result of calling pack on the Label, not the Label itself.)

Once you've done that, you just need to parse the value, which is pretty easy.

Putting it all together:

import tkinter
from tkinter import *

root = Tk()
root.title('Random')
Label(text='How many squares? (ex: 4x4, 5x3)').pack(side=TOP,padx=10,pady=10)

entry = Entry(root, width=10)
entry.pack(side=TOP,padx=10,pady=10)

def onok():
    x, y = entry.get().split('x')
    for row in range(int(y)):
        for col in range(int(x)):
            print((col, row))

Button(root, text='OK', command=onok).pack(side=LEFT)
Button(root, text='CLOSE').pack(side= RIGHT)

root.mainloop()

You just have to change that print to do something useful, like creating the squares.

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

Comments

1

If you don't need an outline for the text box, create_text would be the easiest thing, even though it doesn't have a wrap text feature(at least, in python 3 you can do this):

from tkinter import *
tk = Tk()
canvas = Canvas(tk, 1000, 1000)
canvas.pack()
canvas.create_text(200, 200, text="Example Text")

Try it!

2 Comments

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context.
Oh, I'll take that question out.

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.