简体   繁体   English

更改label文本,Tkinter

[英]Change the label text, Tkinter

Here, is my code snippet:这是我的代码片段:

def message(name, button):
    button['state'] = DISABLED
    mgs_label = ttk.Label(root)

   if button["text"] == "Encryption":

       mgs_label.pack_forget()
       mgs = encryption(name)
       mgs_label = ttk.Label(root, text=mgs).pack(side=LEFT)

   if button["text"] == "Decryption":

      mgs_label.pack_forget()
      mgs = decryption(name)
      mgs_label = ttk.Label(root, text=mgs).pack(side=LEFT)

When i am click the button whether it is encryption button or decryption button it is comes at mentioned position. Here is the snapshot: image1当我单击按钮时,无论是加密按钮还是解密按钮,它都会出现在提到的 position 中。这是快照: image1

And When i am click the another button the text comes after the previous one, i want to remove the previous text and then the last mgs should be displayed.当我单击另一个按钮时,文本出现在前一个按钮之后,我想删除前一个文本,然后应该显示最后一个 mgs。

second snapshot: image2第二张快照: image2

Even i tried to make global variables but then the problem is encryption or decryption is done but the mgs is not showing on the GUI here is the code for that:即使我尝试创建全局变量,但问题是加密或解密已完成,但 mgs 未显示在 GUI 上,这里是代码:

encryption_label = ttk.Label(root)
decryption_label = ttk.Label(root)
def message(name, button):
    button['state'] = DISABLED
    global encryption_label, decryption_label
    if button["text"] == "Encryption":

       if decryption_label.winfo_exists():
          decryption_label.pack_forget()

       mgs = encryption(name)
       encryption_label["text"] = mgs
       encryption_label.pack(side=LEFT)

    if button["text"] == "Decryption":

       if encryption_label.winfo_exists():
          encryption_label.pack_forget()

    mgs = decryption(name)
    decryption_label["text"] = mgs
    decryption_label.pack(side=LEFT)

I suggest creating the label only once and use label.configure to change the text.我建议只创建一次 label 并使用 label.configure 更改文本。 You should provide a working example instead of a code snippet.您应该提供一个工作示例而不是代码片段。 I made one for you.我给你做了一个。 Thank you for your question.谢谢你的问题。

import tkinter as tk
from tkinter import ttk
def e():
    message(None,x_button)
def d():
    message(None,y_button)
def message(name, button):
    #button['state'] = tk.DISABLED

    if button["text"] == "Encryption":
        mgs_label.configure(text = 'encrypted stuff')

    if button["text"] == "Decryption":
        mgs_label.configure(text = 'decrypted stuff')
  
root = tk.Tk()
x_button = tk.Button(root,text = 'Encryption',
                     command = e)
y_button = tk.Button(root,text = 'Decryption',
                    command = d )
mgs_label = ttk.Label(root)
y_button.pack()
x_button.pack()    
mgs_label.pack()
root.mainloop()

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

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