0

我试图在打印内容之前让我的 tkinter 顶层有一个访问密钥。它不会输出任何错误但是当我提供访问密钥时它不会将内容打印到我的终端。我更改了访问密钥但是我仍然无法打印数据。

从 tkinter 导入 *

def qw():
    global en1
    if en1.get() == 2e2c2v:
        print("You have visa to Paris")
        tp.destroy()  # after printing should close the toplevel window
    else:
        print("you dont have access to print the data")


def login_toplevel():
    global en1
    tp = Toplevel()
    tp.geometry("300x300")
    en1 = Entry(tp)
    en1.pack()
    b = Button(tp, text="provide key to print", command=qw)
    b.pack()


root = Tk()
root.geometry("400x400")

b = Button(root, text= "print", command=login_toplevel).pack()

root.mainloop()
4

3 回答 3

1

好吧,从您提供的示例中(在我修复了缩进之后),我得到一个“无效的语法”错误,因为您试图与一个您没有声明en1.get()的变量进行比较。2e2c2v

更新这一行。. .

if en1.get() == 2e2c2v:

. . . 到 。. .

if en1.get() == "2e2c2v":

在我这样做之后,它按预期对我有用。

于 2017-11-15T16:48:34.450 回答
1

有几个问题:

  • 你的 else 行的缩进不好(但我认为这是复制/粘贴的错误)

  • if en1.get() == 2e2c2v:应该if en1.get() == "2e2c2v":

  • 在上一次更正后输入右键时出现错误,因为 tp 不是全局的

更正后的完整代码应该是:

from tkinter import *


def qw():
    global en1
    global tp
    if en1.get() == "2e2c2v":
        print("You have visa to Paris")
        tp.destroy()  # after printing should close the toplevel window
    else:
        print("you dont have access to print the data")


def login_toplevel():
    global en1
    global tp
    tp = Toplevel()
    tp.geometry("300x300")
    en1 = Entry(tp)
    en1.pack()
    b = Button(tp, text="provide key to print", command=qw)
    b.pack()


root = Tk()
root.geometry("400x400")

b = Button(root, text= "print", command=login_toplevel).pack()
root.mainloop()

编辑:

顺便说一句,我认为使用类而不是使用全局会更好。它会给出类似的东西:

import tkinter as tk

class Example:
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("400x400")

        tk.Button(self.root, text= "print", command=self.login_toplevel).pack()

    def start(self):
        self.root.mainloop()

    def qw(self):
        if self.en1.get() == "2e2c2v":
            print("You have visa to Paris")
            self.tp.destroy()  # after printing should close the toplevel window
        else:
            print("you dont have access to print the data")


    def login_toplevel(self):
        self.tp = tk.Toplevel()
        self.tp.geometry("300x300")
        self.en1 = tk.Entry(self.tp)
        self.en1.pack()
        b = tk.Button(self.tp, text="provide key to print", command=self.qw)
        b.pack()

Example().start()
于 2017-11-15T17:02:56.200 回答
0

将您的访问密钥更改为字符串“2e2c2v”,然后为顶级窗口声明全局变量global en1, tp并为您的两个函数解析它

from tkinter import *


def qw():
    global en1, tp
    if en1.get() == "2e2c2v":
        print("You have visa to Paris")
        tp.destroy()  # after printing should close the toplevel window
    else:
        print("you dont have access to print the data")


def login_toplevel():
    global en1, tp
    tp = Toplevel()
    tp.geometry("300x300")
    en1 = Entry(tp)
    en1.pack()
    b = Button(tp, text="provide key to print", command=qw)
    b.pack()


root = Tk()
root.geometry("400x400")

b = Button(root, text= "print", command=login_toplevel).pack()

root.mainloop()
于 2017-11-15T17:43:02.237 回答