如何使得点第二个按钮 ,先清空第一个按钮的label和text,再上第二个按钮的label和text


原代码
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
class MY_GUI():
def init(self,init_window_name):
self.init_window_name = init_window_name
def set_init_window(self):
self.init_window_name.title(“得分”)
self.init_window_name.minsize(1200,500)
self.init_window_name.maxsize(1200,500)
self.str_trans_to_md5_button = Button(self.init_window_name, text=”语文”, bg=”lightblue”, width=25,height=1,command=self.str_command)
self.str_trans_to_md5_button.grid(row=2, column=1)
self.str_trans_to_md5_button = Button(self.init_window_name, text=”数学”, bg=”lightblue”, width=25,height=1,command=self.str_command6)
self.str_trans_to_md5_button.grid(row=2, column=3)
def str_command(self):
self.label1 = Label(self.init_window_name, text=”张三”)
self.label1.grid(row=4, column=1)
self.Text1 = Text(self.init_window_name, width=25, height=1)
self.Text1.grid(row=4, column=3)
self.label2 = Label(self.init_window_name, text=”李四”)
self.label2.grid(row=4, column=5)
self.Text2 = Text(self.init_window_name, width=25, height=1)
self.Text2.grid(row=4, column=7)
self.label3 = Label(self.init_window_name, text=”王五”)
self.label3.grid(row=4, column=9)
self.Text3 = Text(self.init_window_name, width=25, height=1)
self.Text3.grid(row=4, column=11)
self.label4 = Label(self.init_window_name, text=”甲”)
self.label4.grid(row=6, column=1)
self.Text4 = Text(self.init_window_name, width=25, height=1)
self.Text4.grid(row=6, column=3)
self.label5 = Label(self.init_window_name, text=”乙”)
self.label5.grid(row=6, column=5)
self.Text5 = Text(self.init_window_name, width=25, height=1)
self.Text5.grid(row=6, column=7)
self.label6 = Label(self.init_window_name, text=”丙”)
self.label6.grid(row=6, column=9)
self.Text6 = Text(self.init_window_name, width=25, height=1)
self.Text6.grid(row=6, column=11)
def str_command6(self):
self.label1 = Label(self.init_window_name, text=”甲”)
self.label1.grid(row=4, column=1)
self.Text1 = Text(self.init_window_name, width=25, height=1)
self.Text1.grid(row=4, column=3)
self.label2 = Label(self.init_window_name, text=”乙”)
self.label2.grid(row=4, column=5)
self.Text2 = Text(self.init_window_name, width=25, height=1)
self.Text2.grid(row=4, column=7)
self.label3 = Label(self.init_window_name, text=”丙”)
self.label3.grid(row=4, column=9)
self.Text3 = Text(self.init_window_name, width=25, height=1)
self.Text3.grid(row=4, column=11)
self.label4 = Label(self.init_window_name, text=”张三”)
self.label4.grid(row=6, column=1)
self.Text4 = Text(self.init_window_name, width=25, height=1)
self.Text4.grid(row=6, column=3)
self.label5 = Label(self.init_window_name, text=”李四”)
self.label5.grid(row=6, column=5)
self.Text5 = Text(self.init_window_name, width=25, height=1)
self.Text5.grid(row=6, column=7)
self.label6 = Label(self.init_window_name, text=”王五”)
self.label6.grid(row=6, column=9)
self.Text6 = Text(self.init_window_name, width=25, height=1)
self.Text6.grid(row=6, column=11)
if name == ‘main‘:
init_window = Tk()
ZMJ_PORTAL = MY_GUI(init_window)
ZMJ_PORTAL.set_init_window()
init_window.mainloop()

窗口中按钮一的label和按钮二的重叠了,所以我想点按钮二的时候,先清除按钮一中的东西再弄上按钮二的label和text

讨论数量: 3
Jason990420

代码格式错误, 缩格不对, 请参考以下内容, 再重新编辑内容.

代码高亮

```python

你的代码

```

2周前 评论
Jason990420

Most of time, we define variables as Noneas they don’t exist at this moment, like

    def set_init_window(self):
        self.init_window_name.title ("得分")
        self.init_window_name.minsize(1200,500)
        self.init_window_name.maxsize(1200,500)
        self.str_trans_to_md5_button = Button (self.init_window_name, text="语文", bg="lightblue", width=25,height=1,command=self.str_command)
        self.str_trans_to_md5_button.grid(row=2, column=1)
        self.str_trans_to_md5_button = Button (self.init_window_name, text="数学", bg="lightblue", width=25,height=1,command=self.str_command6)
        self.str_trans_to_md5_button.grid(row=2, column=3)
        self.label1 = self.label2 = self.label3 = self.label4 = self.label5 = self.label6 = None
        self.text1 = self.text2 = self.text3 = self.text4 = self.text5 = self.text6 = None

Then you can call method remove_all in your callbacks, like

    def str_command(self):
        self.remove_all()
        ....

    def str_command6(self):
        self.remove_all()
        ....

    def remove_all(self):
        for widget in (self.text1, self.text2, self.text3, self.text4, self.text5, self.text6, self.label1, self.label2, self.label3, self.label4, self.label5, self.label6):
            if widget:
                widget.destroy()


Another way for it

  • Initialize all elements, like all Labels and Text widgets, in your __init__, but not grid them
  • Call method grid_forgetof widget to hide them.
  • Call method grid of widget to show them.
2周前 评论
sadasdaaq (楼主) 1周前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!