Python 如何在 tkinter 中使用文本框并使用值?蟒蛇 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15306197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:44:41  来源:igfitidea点击:

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

pythonuser-interfacetkinter

提问by Kara

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:

如何在 tkinter 的条目小部件中创建多行并使用这些输入来创建一些东西?例如,我想要一个文本框小部件出现并询问用户:

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.

通过用户输入,我想在该特定高度/宽度中创建许多 x 数量的正方形并指定颜色等。我对 tkinter 完全陌生,我不确定如何处理这个问题。

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)

回答by abarnert

You have a number of problems here.

你在这里有很多问题。

I'm not sure what the Squaresclass 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.

我不确定这Squares门课应该做什么,但它基本上什么都不做。您有一堆代码在定义类时运行,创建一些变量(最终将作为类属性,由类的所有实例共享),然后……就是这样。我不会试图弄清楚您在这里打算做什么,我只是要废弃该类并使其全部成为模块级代码。

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

您从不调用root.mainloop(),因此您的程序将只定义一个 GUI,然后从不运行它。

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 commandargument, or .bindit later.

您不会将按钮绑定到任何东西,因此它们不可能产生任何效果。您需要创建某种函数来执行某些操作,然后将其作为command参数传递,或者.bind稍后传递。

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 xvariable, but that's going to be None, because you're setting it to the result of calling packon the Label, not the Labelitself.)

您不存储任何控件的引用,因此以后无法访问它们。如果您想从条目中获取值,您需要某种方式来引用它。(唯一的例外是你的x变量,但是这将是None,因为你将其设置为调用结果的packLabel,而不是Label自己。)

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 printto do something useful, like creating the squares.

你只需要改变它print来做一些有用的事情,比如创建正方形。

回答by python-b5

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

如果您不需要文本框的轮廓,则 create_text 将是最简单的事情,即使它没有换行文本功能(至少,在 python 3 中您可以这样做):

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

Try it!

尝试一下!