Home > Mobile >  Modify a function from another function in Python
Modify a function from another function in Python

Time:10-02

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).

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). 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

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()

CodePudding user response:

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.

If I understand well, you want to go through all the users you get with your config file and change which one you call ?

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. 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

CodePudding user response:

One way of accomplishing what you're after might be for NextAccount to pop the first user/password from the list. This is easier IMO if your OpenFile function gives you a list of [(user1, pass1), ...] rather than [user1, pass1, ...].

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))

CodePudding user response:

In this scenario, I would rather try to:

Give the FileRead function a parameter that indicates which User and Pass to use, like:

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:

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

  • Related