Python Forum
How to swap two numbers in fields in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to swap two numbers in fields in python
#1
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 ?
Reply
#2
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.
Reply
#3
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.
Reply
#4
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?
Reply
#5
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.
Reply
#6
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add two resultant fields from python lambda function klllmmm 4 906 Jun-06-2023, 05:11 PM
Last Post: rajeshgk
  Compare fields from two csv files georgebijum 3 1,367 Apr-25-2022, 11:16 PM
Last Post: Pedroski55
  how to filter two fields in json using python python_student 4 3,665 Mar-15-2021, 05:58 PM
Last Post: python_student
  Swap key and value of a dictionary - and sort it Omid 4 2,835 Oct-28-2020, 01:24 PM
Last Post: Omid
  Json fields not being retrieved mrcurious2020 4 2,053 Sep-14-2020, 06:24 AM
Last Post: bowlofred
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,725 May-09-2019, 12:19 PM
Last Post: Pleiades
  swap elements in list hshivaraj 3 12,531 Apr-22-2019, 09:23 AM
Last Post: Yoriz
  I am trying to swap two variables with a Function.... Jeff_Waldrop 4 3,091 Mar-04-2019, 10:19 AM
Last Post: Jeff_Waldrop
  yes-no RE pattern swap bluefrog 1 2,650 Jun-08-2018, 07:06 AM
Last Post: volcano63
  Mapping Fields zak308 0 2,489 Jan-09-2018, 10:02 PM
Last Post: zak308

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020