关于python3 tkinter command一个bug

Posted 小哈里

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于python3 tkinter command一个bug相关的知识,希望对你有一定的参考价值。

problem

  • ks()函数直接调用可以正确运行
  • 但是放在Button按钮里面,就无法运行了,出现了Kaisa里的Entry无法获取输入的情况
import tkinter as tk
from tkinter import messagebox
from functools import partial

class Kaisa():
    def __init__(self): #构造函数-实例变量
        self.window = tk.Tk()
        self.input = tk.StringVar()
        self.output = tk.StringVar()
        self.key = tk.StringVar()
        self.dzb = ""  #对照表

    def set_window(self):
        self.window.title("凯撒密码")
        self.window.geometry('400x250')
        tk.Label(self.window,text="请输入明文/密文:").place(x=50, y=40)
        tk.Label(self.window, text="请输入位移数:").place(x=50, y=80)
        tk.Label(self.window, text="输出结果:").place(x=50, y=160)
        tk.Label(self.window, textvariable=self.output, bg='red').place(x=50, y=200)
        tk.Entry(self.window, textvariable=self.input).place(x=150, y=40)
        tk.Entry(self.window, textvariable=self.key, width=2).place(x=150, y=80)
        self.key.set(3)
        tk.Button(self.window, bg='lightgreen', activebackground='yellow', text='加密', font=20, command=self.encode).place(x=80, y=120)
        tk.Button(self.window, bg='lightgreen', activebackground='yellow', text='解密', font=20, command=self.decode).place(x=170, y=120)
        tk.Button(self.window, bg='yellow', activebackground='green', text='确定', font=20, command=self.getdzb).place(x=200, y=80)
        self.window.mainloop()

    def getdzb(self):
        print(len(self.input.get())) #Why???
        k = int(self.key.get())
        if(k<0 | k>25):
            messagebox.showwarning(title='警告', message='密钥需要在0-25之间')
            return 
        for i in range(0, 26):
            if i+k < 26:
                self.dzb += chr(i+k+97)
            else:
                self.dzb += chr(i+k+97-26)

    def encode(self):
        input = self.input.get()  #局部变量
        input = input.lower()
        if not(input.isalpha()):  #只由字母组成
            messagebox.showwarning(title='警告', message='你输入的明文不正确')
            return 
        res = ""
        for ch in input:
            res += self.dzb[int(ord(ch))-97] #数值-97
        self.output.set(res)

    def decode(self):
        output = self.output.get()
        output = output.lower()
        if not(output.isalpha()):
            messagebox.showwarning(title='警告', message='你输入的密文不正确')
            return 
        res = ""
        for ch in output:
            res += chr((self.dzb.find(ch))+97)
        self.input.set(res)

if __name__ == "__main__":
    def ks():
        x = Kaisa()
        x.set_window()
    ks()
    
    content = tk.Tk()
    content.title("古典加密系列")
    content.geometry('600x400')
    tk.Button(content,text="凯撒密码",font=40,command=lambda:ks()).place(x=140,y=100)
    content.mainloop()




以上是关于关于python3 tkinter command一个bug的主要内容,如果未能解决你的问题,请参考以下文章

急求:python3 tkinter如何实现点击一个按钮跳出另一个窗口

如何使 Menu.add_command() 在 Mac 上的 tkinter 中工作?

如何给 Python Tkinter 给窗口加标题、改变 button 文本?

tkinter模块常用参数(python3)

Tkinter中的多个键事件绑定 - “Control + E”“Command(apple)+ E”等

Python3 tkinter初级