Kara
Kara

Reputation: 785

how to use text box in tkinter and use the values? python 3

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)

Upvotes: 1

Views: 35051

Answers (2)

python-b5
python-b5

Reputation: 73

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!

Upvotes: 1

abarnert
abarnert

Reputation: 365707

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.

Upvotes: 8

Related Questions