如何在pythontkinter的列表框中显示textfile中的所有项目?

2024-10-05 12:17:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想制作一个列表框来列出用户.conf文本文件。你知道吗

如何列出它来选择要删除的帐户?你知道吗

  • 你知道吗用户.conf地址:
  • 56185用户密码==“56185”
  • 40317用户密码==“40317”
  • 11581用户密码==“11581”
  • 38467用户密码==“38467”
  • 50071用户密码==“50071”
  • 81045用户密码==“81045”

  • 列表框中的首选输出:

  • 56185个
  • 40317号
  • 11581个
  • 38467个
  • 50071个
  • 81045个

    def deleteuser():
       with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "r") as fh:
        lines = fh.readlines()
       with open('C:/FreeRADIUS.net/etc/raddb/users.conf', "w") as fh:
        for line in lines:                  
            if deleteuserentry.get() in line and  deleteuserpwd.get() == "Pa$$w0rd123" :
               tkMessageBox.showinfo("success", "Account Deleted")
               end()
    
    
        deleteteuser = ttk.Label(page1, text="Delete User").grid(column=0, row=6, sticky='W')
        deleteusername = ttk.Label(page1, text="Enter current user's username:").grid(column=0, row=7, sticky='W')
        deleteuserentry = ttk.Listbox(page1, selectmode = BROWSE)
        deleteuserentry.grid(row=7, column=1, columnspan=2)
        for lines in ('C:/FreeRADIUS.net/etc/raddb/users.conf', "r"):
            lb.insert(END,line)
        deleteuserpassword = ttk.Label(page1, text="Enter Administrator's password:").grid(column=0, row=8, sticky='W')
        deleteuserpwd = ttk.Entry(page1, show='*')
        deleteuserpwd.grid(row=8, column=1, columnspan=2)
        deleteuserconfirm = Button(page1, text='Delete this User', borderwidth=2, command= deleteuser)
         deleteuserconfirm.grid(row=9, column=0, sticky='W')
        du = Button(page1, text='import file to delete multiple accounts', borderwidth=2, command= du)
        du.grid(row=9, column=2, sticky='E')
    

Tags: text用户密码netconfetccolumnusers
1条回答
网友
1楼 · 发布于 2024-10-05 12:17:13

您可以通过regex匹配行,在开始处选择用户id,并将行数据填充到一个列表中。当从列表框中删除一个项目时,用户id将附加到另一个列表中。当接口关闭时,会发生从文件中删除的实际操作。这是我的意思的结构。你可以用它作为一个例子-在需要的地方进行优化。你知道吗

import re
import os
from tkinter import *

pattern = re.compile(r'^(?P<user_id>\d+)\s')
data_file = 'users.conf'
res_file = 'result.conf'  # This could alse be the same file from which the users are read

try:
    os.remove(res_file)
except FileNotFoundError:
    pass

# Read users
with open(data_file) as f:
    lines = []

    for l in f.readlines():
        match = pattern.match(l)
        if match is not None:
            user_id = match.group('user_id')
        else:
            user_id = ''
        lines.append((user_id, l))


lines_to_remove = []


def delete_record(lb):
    user_id = lb.get(ANCHOR)
    lines_to_remove.append(user_id)
    lb.delete(ANCHOR)


# Display GUI
top = Tk()
lb = Listbox(top)
b = Button(top, text="Delete",
           command=lambda lb=lb: delete_record(lb))

for line in lines:
    if line[1]:
        lb.insert(END, line[0])

lb.pack()
b.pack()
top.mainloop()

# Delete lines
valid_lines = filter(lambda l: l[0] not in lines_to_remove, lines)
with open(res_file, 'w+') as f:
    f.writelines([l[1] for l in valid_lines])

相关问题 更多 >

    热门问题