gpt4 book ai didi

python - 如何找到字符串中的第三个 ".",然后更改其后面字符的值

转载 作者:行者123 更新时间:2023-12-01 00:02:31 24 4
gpt4 key购买 nike

我有两个条目小部件,其中输入从 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'])

关于python - 如何找到字符串中的第三个 ".",然后更改其后面字符的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60239130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com