Search code examples
pythontkintertkinter-entry

How to find the third "." in a string, then changing the value of character behind it


I have two entry widgets where the input is copied from entry_ip to entry_gateway. entry_gateway is still changeable without affecting entry_ip.

entry_ip the input is users ipv4-address eks: 198.164.65.10

I want the entry_ip to remove the numbers after the last dot. 198.164.65.x and insert "1" instead.

from tkinter import *

my_window = Tk()
my_window.title("IPV4 Configurator")
my_window.configure(background="#2B7A78")
my_window.geometry("400x375+480+340")
my_window.resizable(0, 0)

# Create labels

Label(my_window, text="IP ADDRESS:", bg="#2B7A78", fg="#17252A", font="Caliban 14 bold") \
    .place(x=35, y=70)

Label(my_window, text="SUBNET MASK:", bg="#2B7A78", fg="#17252A", font="Caliban 14 bold") \
    .place(x=35, y=135)

Label(my_window, text="GATEWAY:", bg="#2B7A78", fg="#17252A", font="Caliban 14 bold") \
    .place(x=35, y=200)

# Create StringVar


var_ip = StringVar()
var_mask = StringVar()
var_gateway = StringVar()


# Break function


def focus_next_widget(event):
    event.widget.tk_focusNext().focus()
    return "break"


# Get entry input


def on_entry_input(event):
    text = event.widget.get()
    entry_gateway.delete(0, 'end')
    entry_gateway.insert('1', text)


def user_input_ip():
    input_ip = entry_ip.get()
    display_ip = input_ip
    var_ip.set(display_ip)


# Create ip entry


entry_ip = Entry(my_window, font="Helvetica 15", textvariable=var_ip)
entry_ip.place(x=35, y=100, width=158, height=30)
entry_ip.bind("<Tab>", focus_next_widget)
entry_ip.bind('<KeyRelease>', on_entry_input)


def user_input_mask():
    input_mask = entry_mask.get()
    display_mask = input_mask
    var_mask.set(display_mask)


# Create mask entry


entry_mask = Entry(my_window, font="Helvetica 15", textvariable=var_mask)
entry_mask.place(x=35, y=165, width=158, height=30)
entry_mask.bind("<Tab>", focus_next_widget)
entry_mask.insert(END, "255.255.255.255")


def user_input_gateway():
    input_gateway = entry_gateway.get()
    display_gateway = input_gateway
    var_gateway.set(display_gateway)


# Create gateway entry


entry_gateway = Entry(my_window, font="Helvetica 15", textvariable=var_gateway)
entry_gateway.place(x=35, y=230, width=158, height=30)
entry_gateway.bind("<Tab>", focus_next_widget) 

Solution

  • whatever = '.'.join(entry_ip.split('.')[:3] + ['1'])