Hi,
I have two numbers written in the user field. I would like to swap these numbers in the fields when the swap button is pressed. I did a basic search but I could not find the suitable code for that. Any idea how to implement that feature ?
what do you mean by 'user field'?
It is a variable, a field in a GUI, or some other object (if so, what)
please show us what you've written so far.
Hi, it is basically a user entry field and there are already numbers in them. I would like to have another button which can swap the numbers in the user entry field when the swap button is pressed.
You keep us guessing. I guess you are working with tkinter, am I right?
Then you say the user has entered two numbers. How did he separate them? What if he entered only one number? What if he entered more than two numbers?
Hi, Yes I am using tkinter. There are already numbers in the user entry fields. I just want to have an extra swap button which can swap these two numbers written in the user entry field.
Don't really understand what you want but,
#! /usr/bin/env python3
import tkinter as tk
from functools import partial
def swap(field1, field2):
field1 = field1.get()
field2 = field2.get()
entry1.delete(0, tk.END)
entry2.delete(0, tk.END)
entry1.insert(tk.END, field2)
entry2.insert(tk.END, field1)
root = tk.Tk()
root['pady'] = 5
root['padx'] = 8
field1 = tk.IntVar()
field2 = tk.IntVar()
field1.set(8)
field2.set(22)
label = tk.Label(root, text='Field 1')
label.grid(column=0, row=0)
entry1 = tk.Entry(root)
entry1['textvariable'] = field1
entry1.grid(column=1, row=0)
label = tk.Label(root, text='Field 2')
label.grid(column=0, row=1)
entry2 = tk.Entry(root)
entry2['textvariable'] = field2
entry2.grid(column=1, row=1, pady=8)
btn = tk.Button(root, text='Swap')
btn['command'] = partial(swap, field1, field2)
btn.grid(column=0, columnspan=2, row=3, sticky='we')
root.mainloop()