-3
from tkinter import *

root = Tk()

def oku():
    l1=Label(root,text=0)
    myfilerr=open("kayit.txt","r")
    content=myfilerr.readlines()
    myfilerr.close()
    return content


def kisiSec(kisi):
    okudeg=oku()
    print(kisi)

def main():
    i=0
    okudeg=oku()
    menubar = Menu(root)
    menu2=Menu(menubar,tearoff=0)
    for i in okudeg:
        menu2.add_command(label=i, command=lambda: kisiSec(i))
    root.config(menu=menubar)
    menubar.add_cascade(label="Kişiler", menu=menu2) 

main()



root.mainloop()

Dont judge me im new but there are 3 diffrent names in the text but in main, c1 buttons gives out same name to kisisec() How can i fix that? --ITS TURKISH--

Gandalf
  • 3
  • 2
  • Why you guys disliking it... I want to learn please??!!! – Gandalf Apr 18 '20 at 21:13
  • 1
    Hi! Welcome to StackOverflow. In order to make it easier for others to read your code and therefore be able to help you, it would be a good idea, IMHO, to create a code sample with English names. Also, please be more explicit in your question. – Victor Domingos Apr 18 '20 at 21:14
  • 1
    I am not sure yet about what you mean in your question, but having been myself using tkinter for some years, I strongly advise you to get familiar with Object Oriented Programming, as it really helps make tkinter's verbose code much more manageable as it grows. Please check out Bryan Oakley's advice at https://stackoverflow.com/a/17470842/6167478 – Victor Domingos Apr 18 '20 at 21:33
  • 1
    Read [how-to-understand-closure-in-a-lambda](https://stackoverflow.com/questions/28494089) – stovfl Apr 18 '20 at 21:37
  • Also, the current recommendation on file opening is to use `with open() as xxxx:` like described in this question: https://stackoverflow.com/questions/7395542/is-explicitly-closing-files-important - one nice thing about it is that then you don't need to close the file explicitly and it also takes care of some exceptions. – Victor Domingos Apr 18 '20 at 21:41
  • Thanks for your answers. I will read or watch them. I'm just 15 years old sorry for i didnt use english in names. Please dont jude me :D – Gandalf Apr 18 '20 at 21:44
  • About your kikiSec() function, I noticed that you pass it an argument `kisi` which comes from the `I` in the for loop. Actually if you are coming from other programming languages, please notice that Python's for loop is more like a foreach in C#: it iterates over lists and other iterable objects. So, that 'I' in your for loop is in fact a line of text read from the file. Not sure if that was you intention, but since you assign `i = 0`, probably you intended to use it as a traditional C style for loop index variable. – Victor Domingos Apr 18 '20 at 21:48
  • No problem. StackOverflow is a great place for someone who is in any learning stage. You may find some users nicer that others, but keep focused. And follow the guidelines for question submissions. Always try to make it easier for the others to understand your questions and your code. – Victor Domingos Apr 18 '20 at 21:50
  • Yes, i came here from Dev-C++, i can see my mistake now... Thanks Victor. You helped me more than i deserve... – Gandalf Apr 18 '20 at 21:51

1 Answers1

-1

use this:

lambda i=i:

in for loop, because something about the reference to the incorrect thing, so you need to save it in the lambda, for the function to access the parameter.

rn0mandap
  • 333
  • 1
  • 8
  • Please provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. Also, you've mistyped `lambda` – David Buck Apr 19 '20 at 11:00