Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

python - Python如何从函数内编辑Tkinter标签?(Python How to edit a Tkinter label from within a function?)

I need to change a Tkinter label from inside a function but it isn't passing through correctly because I get the error

(我需要从函数内部更改Tkinter标签,但它无法正确传递,因为出现错误)

AttributeError: 'NoneType' object has no attribute 'config'

(AttributeError:“ NoneType”对象没有属性“ config”)

My code is

(我的代码是)

from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio import SeqIO
from tkinter import *
from tkinter import ttk, filedialog
import os

def select_file(fileLabel):
    filename = filedialog.askopenfilename(initialdir=".")
    name, extension = os.path.splitext(filename)
    extension=''.join(extension.split('.', 1))
    for seq_record in SeqIO.parse(filename, extension):
        sequence = seq_record.seq
    fileBase=os.path.basename(os.path.normpath(filename))
    fileLabel.config(text=fileBase)


tk = Tk()

fileLabel = ttk.Label(text = "No File Selected").pack()

ttk.Button(text = "Select FASTA/Genbank",command = lambda: select_file(fileLabel)).pack()

tk.mainloop()

Does anyone know what I'm doing wrong?

(有人知道我在做什么错吗?)

Edit: The error is on the line

(编辑:错误在行)

fileLabel.config(text=fileBase)
  ask by Jack Ashford translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this :

(尝试这个 :)

fileLabel = ttk.Label(text = "No File Selected")
fileLabel.pack()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...