使用Tkinter将文本框输入打印到控制台(python3.7)

时间:2018-11-28 16:54:13

标签: python python-3.x tkinter printing

我正在使用tkinter在python中制作井字游戏,并且该代码全部有效,除了当我尝试打印哪个玩家是获胜者时,我得到以下输出:

<main class="h-100">
    <div class="container h-100">
        <div class="row justify-content-center align-items-center h-100">
            <div class="col-12">
                <div class="card mx-auto d-block shadow-md-large w-md-80 w-lg-50">
                    <div class="card-header bg-transparent border-0 py-md-5">
                        <img src="/img/logo/aliansce_full.svg" alt="Aliansce Shopping Centers" class="CLI img-fluid mx-auto d-block" autocomplete="on" autofocus require>
                    </div>
                    <div class="card-body px-md-5">
                        <form autocomplete="on">
                            <div class="form-group EGLF mt-4 mt-md-0">
                                <input type="text" id="FLUI" class="form-control FI EFLI border-top-0 border-right-0 border-left-0 rounded-0 shadow-none" autocomplete="on" autofocus required>
                                <!-- FLOAT LABEL EFFECT -->
                                <label fo="FLUI" id="FLUL" class="EFL">Usuário</label>
                                <span class="EFHL"></span>
                                <span class="EFBL"></span>
                                <!-- FRONTEND RESPONSE ERROR MESSAGE -->
                                <div id="FBMEU" class="FBMEU ml-1 text-danger"></div>
                            </div>
                            <div class="form-group EGLF mt-4">
                                <input type="password" id="FLUPI" class="form-control FI EFLI border-top-0 border-right-0 border-left-0 rounded-0 shadow-none" min="1" maxlength="10" required>
                                <!-- FLOAT LABEL EFFECT -->
                                <label for="FLUPI" id="FLUPL" class="EFL">Senha</label>
                                <span class="EFHL"></span>
                                <span class="EFBL"></span>
                                <!-- FRONTEND RESPONSE ERROR MESSAGE -->
                                <div id="FBMEUP" class="ml-1 text-danger"></div>
                            </div>
                            <div class="form-group mt-5">
                                <button type="button" id="FBSL" class="btn btn-primary w-esm-100 mb-2 px-lg-5">LOGIN</button>
                                <button type="button" class="btn bg-light w-esm-100 px-lg-5">VOLTAR</button>
                            </div>
                        </form>
                        <!-- BACKEND RESPONSE ERROR MESSAGE -->
                        <div class="text-center text-danger">
                            Usuário ou senha incorretos.
                            <br> Caso esteja com dificuldades, clique em "Precisa de Ajuda?".
                        </div> 
                    </div>
                    <div class="card-footer bg-white border-0">
                        <button type="button" class="btn btn-link mx-auto d-block">Precisa de Ajuda?</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

我用来打印获胜者的代码是:

The player who won is:  
.!entry

其中 print("The player who won is: ", e1_entry) ... 的定义如下:

e1_entry

和e1是:

e1_entry = e1.get()

我该如何做,以便将来自tkinter e1 = tkinter.Entry(window, textvariable=e1_entry) 的文本打印到控制台上?

谢谢

Gaurav Bhalla

2 个答案:

答案 0 :(得分:3)

这是一个可行的示例。您需要定义一个函数来获取字符串

import tkinter


def getWinner():
    n = entry1.get()

    if n == "":
        n = "Nobody won"

    print(n)

root = tkinter.Tk()

entry1 = tkinter.Entry(root)
entry1.grid(row = 0, column = 0)

root.after(10000, getWinner)

root.mainloop()

它具有一个Entry小部件,并在10秒钟后将调用函数“ getWinner”。如果Entry小部件中没有文本,将没有赢家。

希望这会有所帮助!

答案 1 :(得分:1)

您应该尝试类似的操作

window = Tk()
text = StringVar()
e1 = Entry(window, textvariable = text)
e1.pack()
e1_entry = text.get()
print("The player who won is:", e1_entry)