我试图创建一个简单的tkinter应用程序,复制2个文件夹中的一个,每个文件夹都分配给不同的单选按钮,到一个新创建的路径,用户选择名称,然后从列表框中加入预定义的路径。创建和复制工作正常,当我选择单选按钮2号时,它仍然使用与单选按钮1关联的源路径。我不确定哪里出错了,希望得到任何帮助,代码可以在下面找到。
'
import os
import tkinter as tk
import shutil
# Create the main window
root = tk.Tk()
canvas1 = tk.Canvas(root, width=400, height=300)
canvas1.pack()
# Create an entry box for a new folder name
entry1 = tk.Entry(root)
canvas1.create_window(200, 140, window=entry1)
# Create a StringVar to hold the value of the selected radio button
selected_src = tk.StringVar()
# Create a listbox to hold the destination path
dest_listbox = tk.Listbox(root)
dest_listbox.insert(tk.END, 'C:/Users/UXL8400/Destination1')
dest_listbox.insert(tk.END, 'C:/Users/UXL8400/Destination2')
dest_listbox.insert(tk.END, 'C:/Users/UXL8400/Destination3')
# Create two radio buttons
rb1 = tk.Radiobutton(root, text='Option 1', variable=selected_src, value='C:/Users/UXL8400/Testing/manaburn')
rb2 = tk.Radiobutton(root, text='Option 2', variable=selected_src, value='C:/Users/UXL8400/Testing/mana')
# Create a function to be called when the button is clicked
def copy_folder():
# Get the selected source and destination paths
entrystring = entry1.get()
src = selected_src.get()
dest = dest_listbox.get(tk.ACTIVE)
path = os.path.join(dest, entrystring)
# Use shutil to copy the folder from the source to the destination
shutil.copytree(src, path)
# Create a button to trigger the copy operation
copy_button = tk.Button(root, text='Copy Folder', command=copy_folder)
# Pack the widgets
rb1.pack()
rb2.pack()
dest_listbox.pack()
copy_button.pack()
# Run the main loop
root.mainloop()
'
已尝试在单选按钮上使用锚方法,但仍出现ypeError:StringVar.get()接受1个位置参数,但给出了2个
1条答案
按热度按时间t98cgbkg1#
我修改了一些代码片段。我在
tk.StringVar()
中添加了参数修改代码:
顺便说一句,我不用
shutil
。这要由你来测试。输出: