Python GUI无法正常工作[关闭](Python GUI not working [closed])

编程入门 行业动态 更新时间:2024-10-28 07:34:20
Python GUI无法正常工作[关闭](Python GUI not working [closed])

我试图在Python中创建一个GUI但由于某种原因它无法正常工作。 GUI应该有一个信息按钮和一个退出按钮。 我知道这是一个简单的错误,我应该能够调试,但我找不到它。

回溯错误消息:

在gu = GUI()

self.info_button = tkinter.Button(self.bottom_frame,text =“Show Info”,command = self.showinfo)AttributeError:'GUI'对象没有属性'showinfo'

这是我的代码:

import tkinter import tkinter.messagebox class GUI: def __init__(self): #main window self.main_window = tkinter.Tk() #create two frames self.top_frame= tkinter.Frame() self.bottom_frame = tkinter.Frame() #object of StringVar class self.value = tkinter.StringVar() #top frame label self.info_label = tkinter.Label(self.top_frame,textvariable = self.value) #information button that displays the information about the name and address self.info_button = tkinter.Button(self.bottom_frame, text="Show Info", command=self.showinfo) #quit button that closes the program self.quit_button = tkinter.Button(self.bottom_frame, text ='Quit', command=self.main_window.destroy) #pack method for packing widgets self.info_label.pack() self.info_button.pack(side='left') self.quit_button.pack(side='left') self.top_frame.pack() self.bottom_frame.pack() #main loop for running the program tkinter.mainloop() def showinfo(self): inf = '\tSteven Marcus\n\t274 Baily Drive\n\tWaynesville, NC 27999' self.value.set(inf) #call the main function gu=GUI()

I am trying to create a GUI in Python but for some reason it is not working. The GUI should have an info button and a quit button. I know it is a simple error that I should be able to debug but I can't find it.

Traceback error message:

in gu=GUI()

self.info_button = tkinter.Button(self.bottom_frame, text="Show Info", command=self.showinfo) AttributeError: 'GUI' object has no attribute 'showinfo'

Here is my code:

import tkinter import tkinter.messagebox class GUI: def __init__(self): #main window self.main_window = tkinter.Tk() #create two frames self.top_frame= tkinter.Frame() self.bottom_frame = tkinter.Frame() #object of StringVar class self.value = tkinter.StringVar() #top frame label self.info_label = tkinter.Label(self.top_frame,textvariable = self.value) #information button that displays the information about the name and address self.info_button = tkinter.Button(self.bottom_frame, text="Show Info", command=self.showinfo) #quit button that closes the program self.quit_button = tkinter.Button(self.bottom_frame, text ='Quit', command=self.main_window.destroy) #pack method for packing widgets self.info_label.pack() self.info_button.pack(side='left') self.quit_button.pack(side='left') self.top_frame.pack() self.bottom_frame.pack() #main loop for running the program tkinter.mainloop() def showinfo(self): inf = '\tSteven Marcus\n\t274 Baily Drive\n\tWaynesville, NC 27999' self.value.set(inf) #call the main function gu=GUI()

最满意答案

Python中的缩进很重要,你的缩进是关闭的,你需要缩进整个def showinfo(self): block以便它在GUI类下面。

import tkinter import tkinter.messagebox class GUI: def __init__(self): #main window self.main_window = tkinter.Tk() #create two frames self.top_frame= tkinter.Frame() self.bottom_frame = tkinter.Frame() #object of StringVar class self.value = tkinter.StringVar() #top frame label self.info_label = tkinter.Label(self.top_frame,textvariable = self.value) #information button that displays the information about the name and address self.info_button = tkinter.Button(self.bottom_frame, text="Show Info", command=self.showinfo) #quit button that closes the program self.quit_button = tkinter.Button(self.bottom_frame, text ='Quit', command=self.main_window.destroy) #pack method for packing widgets self.info_label.pack() self.info_button.pack(side='left') self.quit_button.pack(side='left') self.top_frame.pack() self.bottom_frame.pack() #main loop for running the program tkinter.mainloop() def showinfo(self): # indent these: inf = '\tSteven Marcus\n\t274 Baily Drive\n\tWaynesville, NC 27999' self.value.set(inf) #call the main function gu=GUI()

Indentation in Python matters a lot, your indentation is off, you will need to indent the whole def showinfo(self): block so it goes under the GUI class.

import tkinter import tkinter.messagebox class GUI: def __init__(self): #main window self.main_window = tkinter.Tk() #create two frames self.top_frame= tkinter.Frame() self.bottom_frame = tkinter.Frame() #object of StringVar class self.value = tkinter.StringVar() #top frame label self.info_label = tkinter.Label(self.top_frame,textvariable = self.value) #information button that displays the information about the name and address self.info_button = tkinter.Button(self.bottom_frame, text="Show Info", command=self.showinfo) #quit button that closes the program self.quit_button = tkinter.Button(self.bottom_frame, text ='Quit', command=self.main_window.destroy) #pack method for packing widgets self.info_label.pack() self.info_button.pack(side='left') self.quit_button.pack(side='left') self.top_frame.pack() self.bottom_frame.pack() #main loop for running the program tkinter.mainloop() def showinfo(self): # indent these: inf = '\tSteven Marcus\n\t274 Baily Drive\n\tWaynesville, NC 27999' self.value.set(inf) #call the main function gu=GUI()

更多推荐

本文发布于:2023-07-07 13:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1063907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   GUI   Python   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!