简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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