简体   繁体   English

从Python中的另一个function修改一个function

[英]Modify a function from another function in Python

I hope everyone's having a good day, So I have this code that loads a text file, reads all the data.我希望每个人都过得愉快,所以我有这段代码可以加载文本文件,读取所有数据。 assigns each line to a different variable.将每一行分配给不同的变量。 I want to be able to change (for example) the current_user.config(text=User1) in FileRead function to current_user.config(text=User2) whenever I call the function NextAccount so I can sort of print each set of user and pass on screen (or do something with them).每当我调用 function NextAccount 时,我希望能够将 FileRead function 中的 current_user.config(text=User1) 更改(例如)为 current_user.config(text=User2) 这样我就可以打印每组用户并通过在屏幕上(或用它们做点什么)。

Edit: Should've mentioned I'm a beginner so I'm probably not doing this the best way.编辑:应该提到我是初学者所以我可能不是最好的方式。 My program is basically supposed to read around 30 combinations of user/pass and I want to display the first one first and then use a button to navigate through (Next account, previous account).我的程序基本上应该读取大约 30 种用户/密码组合,我想先显示第一个,然后使用按钮浏览(下一个帐户,上一个帐户)。 I wanted to assign each to a different variable just because I want to use pyautogui to copy paste these combinations to a field in another program我想将每个分配给不同的变量只是因为我想使用 pyautogui 将这些组合复制粘贴到另一个程序中的字段

from tkinter import *
from tkinter import filedialog as fd

file_path = ''
datalist = []
   
def OpenFile():
    global file_path
    file_path = fd.askopenfilename()
    FileRead()
    
def FileRead():
   
    data = open(file_path)
    datalist = data.readlines()
    
    User1 = datalist[0]
    Pass1 = datalist[1]
    User2 = datalist[2]
    Pass2 = datalist[3]
    User3 = datalist[4]
    Pass3 = datalist[5]
    #.....so on
    current_user.config(text=User1)  #<<<THESE TWO VALUES WHEN function NextAccount is called
    current_pass.config(text=Pass1)  #<<<
    data.close()
    
def NextAccount():
    #I want THIS func to be able to change the FileRead function...


    
window = Tk()
window.geometry('600x600')
window.config(bg='black')

file_button = Button(window,text='Select File', command=OpenFile)
file_button.pack()

current_user = Label(window)
current_user.pack()
current_pass = Label(window)
current_pass.pack()

next_acc_button = Button(window,command= NextAcc)

window.mainloop()

I'm not sure to understand well what are you asking for.我不确定你在要求什么。

First of all, if you read a config file, maybe you should have a look on configparser , your code will be more readable as it is a json like way to get config.首先,如果你阅读配置文件,也许你应该看看configparser ,你的代码将更具可读性,因为它是一种类似于 json 的获取配置的方式。

If I understand well, you want to go through all the users you get with your config file and change which one you call?如果我理解得很好,您想通过配置文件获得的所有用户 go 并更改您调用的用户?

If yes, put your users into a list and create an interator on that list.如果是,请将您的用户放入列表并在该列表上创建一个交互器。

user1 = {"username": "user1", "password": "1234"}
user2 = {"username": "user2", "password": "4567"}

users = [user1, user2]
itr_users = iter(users)

then, when you call your function, just call itr_users.next() to get the next item of the users list and do your stuff.然后,当您致电 function 时,只需调用itr_users.next()即可获取用户列表的下一项并执行您的操作。 You should be able to access users informations this way您应该能够通过这种方式访问用户信息

def next_item():
    curr_user = next(itr_users)
    curr_user["username"]
# First call
#   > user1
# Second call
#   > user2

In this scenario, I would rather try to:在这种情况下,我宁愿尝试:

Give the FileRead function a parameter that indicates which User and Pass to use, like:FileRead function 一个参数,指示使用哪个 User 和 Pass,例如:

def FileRead(n):
    data = open(file_path)
    datalist = data.readlines()

    user_pass_list = [(datalist[i], datalist[i+1]) for i in range( ... )]

    #.....so on
    current_user.config(text=user_pass_list[n][0])  #<<<THESE TWO VALUES WHEN function NextAccount is called
    current_pass.config(text=user_pass_list[n][1])  #<<<
    data.close()

Or set a global variable that the FileRead function will use:或者设置FileRead function 将使用的全局变量:

n_user_pass = 0

def FileRead():
    data = open(file_path)
    datalist = data.readlines()

    user_pass_list = [(datalist[i], datalist[i+1]) for i in range( ... )]

    #.....so on
    current_user.config(text=user_pass_list[n][0])  #<<<THESE TWO VALUES WHEN function NextAccount is called
    current_pass.config(text=user_pass_list[n][1])  #<<<
    data.close()

def NextAccount():
    global n_user_pass
    n_user_pass = ...

I changed the way you stored your user and passes, to make it into a list [(user1, pass1), ... ] that you can access through indices我更改了您存储用户和通行证的方式,将其放入列表 [(user1, pass1), ... ] 中,您可以通过索引访问该列表

One way of accomplishing what you're after might be for NextAccount to pop the first user/password from the list.完成您所追求的任务的一种方法可能是让NextAccount从列表中pop第一个用户/密码。 This is easier IMO if your OpenFile function gives you a list of [(user1, pass1), ...] rather than [user1, pass1, ...] .如果您的OpenFile function 为您提供[(user1, pass1), ...]而不是[user1, pass1, ...]的列表,这在 IMO 中会更容易。

I might structure it something like this:我可能会像这样构造它:

datalist = []

def FileRead(file_path: str) -> list[tuple[str, str]]:
    """Reads file_path, returns list of (user, passwd) tuples."""
    with open(file_path) as data:
        datalist = data.readlines()
    return [
        (user, passwd)
        for user, passwd in zip(datalist[::2], datalist[1::2])
    ]

def OpenFile() -> None:
    """Asks user for a filename, read user/password data, and
    add all data from the file into datalist."""
    file_path = fd.askopenfilename()
    datalist.extend(FileRead(file_path))

def NextAccount() -> None:
    """Print the current user/password and pop it from datalist."""
    print(datalist.pop(0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM