Treeschapter
Treeschapter

Reputation: 48

Using Variables with a tkinter button

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.

Upvotes: 0

Views: 176

Answers (1)

Adam Smith
Adam Smith

Reputation: 54233

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

Upvotes: 2

Related Questions