简体   繁体   English

解决如何在Tkinter中为我的Python程序更新标签的问题

[英]Issues figuring out how to update labels in Tkinter for my Python program

So I am writing a Python program in class that uses the Caesar cipher to take a users input and output it as cipher-text. 因此,我在类中编写了一个Python程序,该程序使用Caesar密码来接受用户输入并将其作为密文输出。 Since i had a lot more time for this project I planned on giving it a GUI in Tkinter. 由于我有更多的时间参与该项目,因此我计划在Tkinter中为其提供GUI。 But when I assign the resulted cipher-text to a label it won't display it and keeps it blank. 但是,当我将结果密文分配给标签时,它将不会显示它并将其保留为空白。 I'm a noob to python and even more to Tkinter so I'm not too keen on being able to fix these issues myself. 我对python不熟悉,对Tkinter则更不熟悉,因此我不太想自己解决这些问题。 Here's the code: 这是代码:

import string
import collections
import random
import tkinter
from tkinter import *
from tkinter.ttk import *

root = Tk()
root.title("Encoder")
root.geometry("500x400")

def caesar(rotate_string, number_to_rotate_by):

    upper = collections.deque(string.ascii_uppercase)
    lower = collections.deque(string.ascii_lowercase)

    upper.rotate(number_to_rotate_by)
    lower.rotate(number_to_rotate_by)

    upper = ''.join(list(upper))

    lower = ''.join(list(lower))

    return rotate_string.translate(str.maketrans(string.ascii_uppercase, upper)).translate(str.maketrans(string.ascii_lowercase, lower))

def callback():
    print (code)

b = Button(root, text="get", width=10, command=callback)
b.pack()

var = StringVar()

e = Entry(root, textvariable = var)
e.pack()

our_string = e.get()
random_number = random.randint(1,25)
code = caesar(our_string, random_number)


l = Label(root, textvariable=code, anchor=NW, justify=LEFT, wraplength=398)
l.pack()
l.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()

There are several issues with the code you've posted. 您发布的代码存在几个问题。 First and foremost, your callback doesn't do anything besides print the code variable. 首先,您的回调除了打印code变量外没有做任何事情。 You need to move your call to caesar and the associated code into the callback, like so 您需要将对caesar的调用以及相关代码移至回调中,如下所示

def callback():
    global code
    our_string = e.get()
    random_number = random.randint(1, 25)
    code.set(caesar(our_string, random_number))

The second issue that I see is that you need to use a StringVar as the textvariable argument in your Label constructor in order to get the label to update automatically. 我看到的第二个问题是,您需要在Label构造函数中使用StringVar作为textvariable参数,以使标签自动更新。 When all is said and done, my version of your code looks like 说完一切,我的代码版本看起来像

import string
import collections
import random
from tkinter import *
from tkinter.ttk import *

root = Tk()
root.title("Encoder")
root.geometry("500x400")

code = StringVar()
code.set('Hello')


def caesar(rotate_string, number_to_rotate_by):
    upper = collections.deque(string.ascii_uppercase)
    lower = collections.deque(string.ascii_lowercase)

    upper.rotate(number_to_rotate_by)
    lower.rotate(number_to_rotate_by)

    upper = ''.join(list(upper))

    lower = ''.join(list(lower))

    return rotate_string.translate(str.maketrans(string.ascii_uppercase, upper)).translate(str.maketrans(string.ascii_lowercase, lower))


def callback():
    global code
    our_string = e.get()
    random_number = random.randint(1, 25)
    code.set(caesar(our_string, random_number))

b = Button(root, text="get", width=10, command=callback)
b.pack()

var = StringVar()

e = Entry(root, textvariable=var)
e.pack()
l = Label(root, textvariable=code, anchor=NW, justify=LEFT, wraplength=398)
l.pack()
l.place(relx=0.5, rely=0.5, anchor=CENTER)

root.mainloop()

This seems to do what you'd expect. 这似乎可以达到您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM