鱼C论坛

 找回密码
 立即注册
查看: 2281|回复: 9

[已解决]如何批量新建Button&Entry,点击button选择文件,将路径写入对应的Entry

[复制链接]
发表于 2020-10-22 11:01:34 | 显示全部楼层 |阅读模式

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

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

x

求助: 如何批量新建Button&Entry,点击button选择文件,将路径写入对应的Entry
下面的代码可以批量创建Button&Entry,但是没办法实现点击button选择文件,将路径写入对应的Entry



import tkinter as tk
from tkinter.filedialog import *

root=tk.Tk()
root.geometry('500x300')
iCount=input('請輸入文件個數:')
sFilePath=tk.StringVar()
def btn_com():
    sFilePath.set(askopenfilename())

for i in range(1,int(iCount)+1):
  tk.Button(root,text='請選擇文件'+str(i),command=btn_com).grid(row=i-1,column=0)
  tk.Entry(root,textvariable=sFilePath,width=50).grid(row=i-1,column=1)

mainloop()
最佳答案
2020-10-22 12:08:25
试试这样,name_list = ['a', 'b', 'c', 'd']  # 定义对象名字列表,这句话可以预设添加,以免超范围
  1. import tkinter as tk
  2. from tkinter.filedialog import *

  3. root = tk.Tk()
  4. root.geometry('500x300')
  5. iCount = input('請輸入文件個數:')
  6. name_list = ['a', 'b', 'c', 'd']  # 定义对象名字列表


  7. class file_name():  # 定义类,用来创建循环对象
  8.     def __init__(self):
  9.         self.sFilePath = tk.StringVar()

  10.     def btn_com(self):
  11.         self.sFilePath.set(askopenfilename())


  12. for i in range(1, int(iCount) + 1):
  13.     name_list[i - 1] = file_name()  # 用对象名字列表内元素,创建循环对象
  14.     tk.Button(root, text='請選擇文件' + str(i), command=name_list[i - 1].btn_com).grid(row=i - 1, column=0)
  15.     tk.Entry(root, textvariable=name_list[i - 1].sFilePath, width=50).grid(row=i - 1, column=1)

  16. mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-22 12:08:25 | 显示全部楼层    本楼为最佳答案   
试试这样,name_list = ['a', 'b', 'c', 'd']  # 定义对象名字列表,这句话可以预设添加,以免超范围
  1. import tkinter as tk
  2. from tkinter.filedialog import *

  3. root = tk.Tk()
  4. root.geometry('500x300')
  5. iCount = input('請輸入文件個數:')
  6. name_list = ['a', 'b', 'c', 'd']  # 定义对象名字列表


  7. class file_name():  # 定义类,用来创建循环对象
  8.     def __init__(self):
  9.         self.sFilePath = tk.StringVar()

  10.     def btn_com(self):
  11.         self.sFilePath.set(askopenfilename())


  12. for i in range(1, int(iCount) + 1):
  13.     name_list[i - 1] = file_name()  # 用对象名字列表内元素,创建循环对象
  14.     tk.Button(root, text='請選擇文件' + str(i), command=name_list[i - 1].btn_com).grid(row=i - 1, column=0)
  15.     tk.Entry(root, textvariable=name_list[i - 1].sFilePath, width=50).grid(row=i - 1, column=1)

  16. mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-10-22 15:53:50 | 显示全部楼层
疾风怪盗 发表于 2020-10-22 12:08
试试这样,name_list = ['a', 'b', 'c', 'd']  # 定义对象名字列表,这句话可以预设添加,以免超范围

NB,多谢啦!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-22 16:34:59 | 显示全部楼层
疾风怪盗 发表于 2020-10-22 12:08
试试这样,name_list = ['a', 'b', 'c', 'd']  # 定义对象名字列表,这句话可以预设添加,以免超范围

批量建完后如果我要单独修改按钮上面的文字该怎么写呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-22 16:57:31 | 显示全部楼层
lengyue869 发表于 2020-10-22 16:34
批量建完后如果我要单独修改按钮上面的文字该怎么写呢?

不知道啊。。。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-22 17:18:56 | 显示全部楼层
lengyue869 发表于 2020-10-22 16:34
批量建完后如果我要单独修改按钮上面的文字该怎么写呢?

尝试了下,这样可以,但是有些限制,就是也要添加名字列表,循环修改
  1. import tkinter as tk
  2. from tkinter.filedialog import *

  3. root = tk.Tk()
  4. root.geometry('500x300')
  5. iCount = input('請輸入文件個數:')
  6. name_list = ['a', 'b', 'c', 'd']  # 定义对象名字列表


  7. class file_name():  # 定义类,用来创建循环对象
  8.     def __init__(self, i):
  9.         self.sFilePath = tk.StringVar()
  10.         self.name = f'name{i}'
  11.         print(self.name)

  12.     def btn_com(self):
  13.         self.sFilePath.set(askopenfilename())


  14. for i in range(1, int(iCount) + 1):
  15.     name_list[i - 1] = file_name(i)  # 用对象名字列表内元素,创建循环对象
  16.     name_list[i - 1].name = tk.Button(root, text='請選擇文件' + str(i), command=name_list[i - 1].btn_com)
  17.     name_list[i - 1].name.grid(row=i - 1, column=0)
  18.     tk.Entry(root, textvariable=name_list[i - 1].sFilePath, width=50).grid(row=i - 1, column=1)

  19. file_name_list = ['a', 'b', 'c', 'd']
  20. for i in range(1, int(iCount) + 1):
  21.     name_list[i - 1].name['text'] = file_name_list[i - 1]
  22. mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-10-23 09:01:56 | 显示全部楼层
疾风怪盗 发表于 2020-10-22 17:18
尝试了下,这样可以,但是有些限制,就是也要添加名字列表,循环修改

perfect !!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-26 17:04:31 | 显示全部楼层
疾风怪盗 发表于 2020-10-22 17:18
尝试了下,这样可以,但是有些限制,就是也要添加名字列表,循环修改

再次請教如何實現以下功能

點擊Run按鈕將Entry中的路徑寫入到Excel B列中對應的單元格(因現在權限不夠,沒辦法上傳圖片跟excel附件.....)


import tkinter as tk
from tkinter.filedialog import *
import winreg
import xlwings as xw

root = tk.Tk()
root.geometry('850x400+300+100')
# iCount = int(input('請輸入文件個數:'))
iCount = 3
name_list = ['a', 'b', 'c', 'd','e','f','g']  # 定义对象名字列表

def get_desktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
                         r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
    return winreg.QueryValueEx(key,"Desktop")[0]
desPath=get_desktop()

#=====================================================================
def btn_run():
    app = xw.App(visible=True, add_book=False)
    wb_vba=app.books.open('VBA test.xlsm')
    sht_main=wb_vba.sheets['main']
    for i in range(1,iCount+1):
        sht_main.range(i+3,2).value='Entry中對應的文件路徑'

    # vba=app.macro('VBA_Test')
    # vba.run()

#=====================================================================

class file_name():  # 定义类,用来创建循环对象
    def __init__(self):
        self.sFilePath = tk.StringVar()

    def btn_com(self):
        global desPath
        self.sFilePath.set(askopenfilename())
        # self.sFilePath.set(askopenfilename(initialdir=desPath))

for i in range(1, iCount + 1):
    name_list[i - 1] = file_name()  # 用对象名字列表内元素,创建循环对象
    tk.Label(text="File"+str(i),width=10,bd=8,
             font=('Arial',13)).grid(row=i-1,column=0)
    tk.Button(root, text='選擇文件',bd=2,font=('Arial',10),
              command=name_list[i - 1].btn_com).grid(row=i - 1, column=2)
    tk.Entry(root, textvariable=name_list[i - 1].sFilePath,
             width=70,font=('Arial',13)).grid(row=i - 1, column=1)

btnRun=tk.Button(text='    Run    ',font=('Arial',11),bg='green',fg='white',
                 command=btn_run)
btnRun.grid(row=i,column=1)

mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-27 12:05:37 | 显示全部楼层
lengyue869 发表于 2020-10-26 17:04
再次請教如何實現以下功能

點擊Run按鈕將Entry中的路徑寫入到Excel B列中對應的單元格(因現在權限不夠 ...

不知道额。。。。。。。。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-27 12:28:29 From FishC Mobile | 显示全部楼层
疾风怪盗 发表于 2020-10-27 12:05
不知道额。。。。。。。。。。。


已经解决咯,先将路径写到列表,再写到excel就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-8 02:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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