鱼C论坛

 找回密码
 立即注册
查看: 2360|回复: 3

tkinter Lable排版问题求解

[复制链接]
发表于 2021-3-21 12:02:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
先附上我的代码
  1. import tkinter as tk


  2. class Application(tk.Frame):
  3.     def __init__(self, master=None):
  4.         super().__init__(master)
  5.         self.master = master
  6.         self.pack()
  7.         self.create_widgets()

  8.     def create_widgets(self):
  9.         self.hi_there = tk.Button(self)
  10.         self.hi_there["text"] = "Hello World\n(click me)"
  11.         self.hi_there["command"] = self.say_hi
  12.         self.hi_there.pack(side="top")
  13.         self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
  14.         self.quit.pack(side="bottom")

  15.     def say_hi(self):
  16.         global tips
  17.         tk.Label(self,text=f'你点击了{tips}次了').pack()
  18.         tips += 1


  19. root = tk.Tk()
  20. app = Application(master=root)
  21. tips = 1
  22. app.mainloop()
复制代码

执行结果为(插入不了图片,尴尬):每次点击以后就会自动在下方自动新增一个,如你点击了1次了
你点击了2次了
你点击了3次了
你点击了4次了
你点击了5次了
...

我想达到的效果是我每点击一次就不在下方继续新增,若我点击5次,应该是
...
你点击了5次了
你点击了4次了
你点击了3次了
你点击了2次了
你点击了1次了

有没有大佬指点一下,感谢万分!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-21 12:36:43 | 显示全部楼层
  1. import tkinter as tk


  2. class Application(tk.Frame):
  3.     def __init__(self, master=None):
  4.         super().__init__(master)
  5.         self.master = master
  6.         self.pack()
  7.         self.create_widgets()
  8.         self.v=tk.StringVar()
  9.         tk.Label(self,textvariable=self.v).pack()
  10.         self.tips=1

  11.     def create_widgets(self):
  12.         self.hi_there = tk.Button(self)
  13.         self.hi_there["text"] = "Hello World\n(click me)"
  14.         self.hi_there["command"] = self.say_hi
  15.         self.hi_there.pack(side="top")
  16.         self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
  17.         self.quit.pack(side="bottom")

  18.     def say_hi(self):
  19.         string = '你点击了%s次了'%self.tips
  20.         self.v.set(string)
  21.         self.tips+=1




  22. root = tk.Tk()
  23. app = Application(master=root)
  24. app.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-31 11:56:55 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-31 12:32:44 | 显示全部楼层

你想实现什么,不是在中间格子里增加数字吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-25 18:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表