鱼C论坛

 找回密码
 立即注册
查看: 1878|回复: 14

tkinter:画布上未能成功添加文本的相关问题

[复制链接]
回帖奖励 4 鱼币 回复本帖可获得 2 鱼币奖励! 每人限 1 次(中奖概率 60%)
发表于 2021-10-8 22:37:21 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 黎明丿晓小 于 2021-10-9 15:19 编辑

直接上问题:
      1.现在需要在画布上添加一些文本,或是显示文本的文本框,置于画布底端,但现在创建完成后并未显示文本,附上代码,请各位大佬指明方向。
      2.添加列表框Listbox不能显示在画布右侧,而是紧跟左侧列表框,如何将其放置到画布右侧。
      3.例如help_text文本未能正常显示。

      预期效果图见附件,另附代码
  1. from os import listdir
  2. from tkinter import *
  3. from tkinter import filedialog
  4. from PIL import Image,ImageTk
  5. from tkinter import messagebox

  6. files = {}
  7. img_open = None
  8. img = None

  9. [color=Yellow]help_text="""
  10. 1.打开文件

  11. 2.选择打开文件或文件夹

  12. 3.单击显示图片
  13. """[/color]

  14. root = Tk()
  15. root.title("叶片计数")
  16. root.geometry("600x600")

  17. #-------------------命令----------------------#
  18. def open_file():
  19.     file_paths = filedialog.askopenfilenames()
  20.     list1.delete(0,END)
  21.     files.clear()

  22.     file_list = []
  23.     for file_path in file_paths:
  24.         name = file_path.split("/")[-1]
  25.         file_list.append(name)
  26.         files[name] = file_path
  27.         list1.insert(END,name)

  28. def open_dir():
  29.     dir_path = filedialog.askdirectory()
  30.     list1.delete(0,END)
  31.     files.clear()

  32.     file_names = []
  33.     target_file_name = ["jpg","png","gif"]
  34.     for name in listdir(dir_path):
  35.         if name.split(".")[-1].lower() in target_file_name:
  36.             file_names.append(name)
  37.             files[name] = dir_path+"/"+name
  38.             list1.insert(END,name)

  39. def show_view(event):
  40.     global img_open
  41.     global img
  42.     try:
  43.         back.delete(ALL)
  44.         get_file = list1.get(ACTIVE)
  45.         file_path = files[get_file]
  46.     except:
  47.         return
  48.     back_width = back.winfo_width()
  49.     back_height = back.winfo_height()
  50.     img_open = Image.open(file_path)
  51.     img_open.thumbnail((back_width,back_height),Image.ANTIALIAS)
  52.     img = ImageTk.PhotoImage(img_open)
  53.     back.create_image(back_width / 2, back_height / 2,anchor=CENTER,image=img)

  54. def refresh(event):
  55.     root_width = root.winfo_width()
  56.     root_height = root.winfo_height()

  57.     back.config(width=root_width - 200, height=root_height - 10)
  58.     show_view(1)
  59. def train():
  60.     pop1 = Toplevel()
  61.     pop1.title("测试")
  62.     pop1.geometry("200x200")


  63.     Label(pop1,text="batch").grid(row=0,sticky=E)
  64.     Label(pop1,text="learning_rate").grid(row=1,sticky=E)
  65.     Label(pop1,text="width").grid(row=2, sticky=E)
  66.     Label(pop1, text="height").grid(row=3, sticky=E)
  67.     Label(pop1, text="decay").grid(row=4, sticky=E)
  68.     Label(pop1, text="steps").grid(row=5, sticky=E)
  69.     Label(pop1, text="max_batches").grid(row=6, sticky=E)

  70.     Entry(pop1).grid(row=0,column=1)
  71.     Entry(pop1).grid(row=1,column=1)
  72.     Entry(pop1).grid(row=2, column=1)
  73.     Entry(pop1).grid(row=3, column=1)
  74.     Entry(pop1).grid(row=4, column=1)
  75.     Entry(pop1).grid(row=5, column=1)
  76.     Entry(pop1).grid(row=6, column=1)

  77.     def over():
  78.         messagebox.showinfo("训练", "训练完毕")


  79.     Button(pop1,text="开始训练",command=over).grid(row=7,columnspan=4,pady=5)
  80.     pop1.mainloop()
  81. def test():
  82.     pop2 = Toplevel()
  83.     pop2.title("测试")
  84.     pop2.geometry("200x200")

  85.     frame = LabelFrame(pop2,text="      测试结果      ",padx=5,pady=5)
  86.     frame.pack()

  87.     Label(frame, text="mAP:60.8%",justify=LEFT).grid(row=0,column=0)
  88.     Label(frame, text="Precision:81.3%",justify=LEFT).grid(row=1, column=0)
  89.     Label(frame, text="Recall:76.1%",justify=LEFT).grid(row=2, column=0)
  90.     Label(frame, text="F1-score:78.6%",justify=LEFT).grid(row=3, column=0)
  91.     pop2.mainloop()
  92. def val():
  93.     pop3 = Toplevel()
  94.     pop3.title("检测")
  95.     pop3.geometry("200x200")

  96.     frame = LabelFrame(pop3, text="      检测结果      ", padx=5, pady=5)
  97.     frame.pack()


  98.     pop3.mainloop()
  99. #-------------------菜单栏--------------------#
  100. menubar = Menu(root)
  101. file_menu = Menu(menubar,tearoff=0)
  102. file_menu.add_command(label="打开文件",command=open_file)
  103. file_menu.add_command(label="打开文件夹",command=open_dir)
  104. file_menu.add_separator()
  105. file_menu.add_command(label="退出",command=root.quit)

  106. menubar.add_cascade(label="文件",menu=file_menu)

  107. menubar.add_command(label="训练",command=train)

  108. #test_menu = Menu(menubar,tearoff=0)
  109. #menubar.add_cascade(label="测试",menu=test_menu)
  110. menubar.add_command(label="测试",command=test)

  111. menubar.add_command(label="检测",command=val)
  112. #detect_menu = Menu(menubar,tearoff=0)
  113. #menubar.add_cascade(label="检测",menu=detect_menu)

  114. root.config(menu=menubar)
  115. #------------------画布-----------------------#
  116. back = Canvas(root,bg="white",width=400,height=600)
  117. back.pack(side=RIGHT,fill=BOTH,expand=True)
  118. back.create_text(200,300,anchor=CENTER,text=[color=Yellow]help_text[/color])


  119. #------------------滚动条---------------------#
  120. sb = Scrollbar(root)
  121. sb.pack(side=RIGHT,anchor=CENTER,fill=Y)
  122. #-------------------列表框--------------------#
  123. list1 = Listbox(root,yscrollcommand=sb.set)
  124. list1.pack(side=LEFT,fill=Y)
  125. list1.bind("<Button-1>",show_view)
  126. sb.config(command=list1.yview)


  127. root.bind("<Configure>",refresh)
  128. root.mainloop()
复制代码

预期效果图

预期效果图
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-9 15:20:45 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-10 21:04:39 | 显示全部楼层

回帖奖励 +2 鱼币

蹭个鱼币,顺便帮顶一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-11 11:50:26 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-11 12:41:31 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-11 19:33:30 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-11 22:48:23 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-15 11:53:28 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-15 12:23:44 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-15 14:53:59 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-15 19:14:16 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-15 20:56:20 | 显示全部楼层

回帖奖励 +2 鱼币

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-15 23:02:29 | 显示全部楼层

回帖奖励 +2 鱼币

   q
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-15 23:03:32 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-15 23:04:03 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-12 00:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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