简体   繁体   中英

Python/Tkinter not getting value from Radio Buttons

I am trying to create a simple tkinter application that copies one of 2 folders which are each assigned to a different radio button, to a newly created path of which the user chooses the name, which is then joined with a predefined path from a listbox. The creation and copy works fine only, when I select radio button number 2 it still uses the source path associated to radio button 1. I'm not sure where things are going wrong any help would be appreciated the code can be foun below.

`

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()

`

Tried with the Anchor method on the radio button but keep on getting ypeError: StringVar.get() takes 1 positional argument but 2 were given

Does this help? I modified some snippet code. I added parameter in tk.StringVar()

Modified code:

selected_src = tk.StringVar(root, '1')

rb1 = tk.Radiobutton(root, text='Option 1', variable=selected_src, value='manaburn')
rb2 = tk.Radiobutton(root, text='Option 2', variable=selected_src, value='mana')

def copy_folder():
    if (selection := selected_src.get()) == "manaburn":
       src = selected_src.get()
       print(src)
       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)
    elif selection == "mana":
         src = selected_src.get()
         print(src)
         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)  

Btw, I don't use shutil . It is up to you to test.

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM