0

I am making a program which a user can create his own tkinter buttons. However I have a problem with the custom name. It creates a name by storing it in a variable however it completely ignores the variable even when it is a direct variable. E.g: variable = "TEXT HERE"

Folder = open(fold2, "r")
Title = Folder.readline(1)
FolderBNam = Button(self, anchor=tk.W, text=Title, command= lambda: self.controller.show_frame(FoldButton1))
FolderBNam.place(height=55, width=75,x=25,y=100)
Folder.close

I have searched for answers of course and even tried to use Lamdba which didn't go so good.

2
  • Why are you doing readline(1)? Commented Dec 31, 2015 at 18:50
  • @AdamSmith I am trying to store the name in a txt file. So if the user restarts the program his buttons will remain there. Commented Dec 31, 2015 at 18:52

1 Answer 1

2

The only immediate problem I see is file.readline shouldn't be called with an argument. That should be giving you one character rather than one line (equivalent to Folder.read(1). Check my edited code below, also edited to look more like Python:

import tkinter as tk
from tkinter import ttk

with open(fold2) as f:
    title = f.readline()  # no argument
f_bnam = ttk.Button(self, anchor=tk.W, text=title, command=...)
f_bnam.place(...)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! So if i change put a 2. E.G: f.readline(2) Will it still work?
@MarsOne um, what do you think the number is doing?
@MarsOne it will if you do readline(), but if you do readline(n) it doesn't guarantee that it will read the whole line, because it will only take up to n characters of text.
See the docs and read carefully about the size argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.