如何找到第三个“。”在一个字符串中,然后改变它后面的字符值

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

我有两个输入小部件,其中输入从 entry_ip 复制到 entry_gatewayentry_gateway 仍然可以更改而不影响 entry_ip

entry_ip 输入用户 ipv4-address eks: 198.164.65.10

我希望 entry_ip 删除最后一个点之后的数字。 198.164.65.x 并改为插入“1”。

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) 
whatever = '.'.join(entry_ip.split('.')[:3] + ['1'])

使用正则表达式

import re
new_ip = re.sub(r"\d+$", "1", entry_ip)