#python
#python
Вопрос:
Ниже приведена простая рабочая программа с графическим интерфейсом. Однако, когда я пытаюсь открыть его из bat
файла, он выдает ошибку.
Bat-файл (2 строки):
ch10.2.py
pause
Ошибка, которую я получаю,:
[сообщение об ошибке в текстовом формате должно быть включено здесь]
Мой код:
# Lazy Buttons 2
# Demonstrates using a class with Tkinter
from tkinter import *
class Application(Frame):
""" A GUI application with three buttons. """
def __init__(self, master):
""" Initialize the Frame. """
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create three buttons that do nothing. """
# create first button
self.bttn1 = Button(self, text = "I do nothing!")
self.bttn1.grid()
# create second button
self.bttn2 = Button(self)
self.bttn2.grid()
self.bttn2.configure(text = "Me too!")
# create third button
self.bttn3 = Button(self)
self.bttn3.grid()
self.bttn3["text"] = "Same here!"
# main
root = Tk()
root.title("Lazy Buttons 2")
root.geometry("200x85")
app = Application(root)
root.mainloop()
Комментарии:
1. В чем ошибка? И какую точную команду вы используете, чтобы запустить ее без ошибок, и установлены ли у вас как 2.x, так и 3.x.
2. Пожалуйста, не включайте скриншоты текста в свои сообщения — вставьте фактическую ошибку здесь.
3. Может быть, ваш основной интерпретатор не Python 3.5? 2.7 или что-то более старое?
4. должен ли ваш файл .bat начинаться с
python
Ответ №1:
Он выдает ошибку super(Application, self).__init__(master)
.
Если вы замените на Frame.__init__(self, master)
, он работает. Смотрите ниже
# Lazy Buttons 2
# Demonstrates using a class with Tkinter
from tkinter import *
class Application(Frame):
""" A GUI application with three buttons. """
def __init__(self, master):
""" Initialize the Frame. """
Frame.__init__(self, master)
#super(Application, self).__init__(master)
self.grid()
self.createWidgets()
def createWidgets(self):
""" Create three buttons that do nothing. """
# create first button
self.bttn1 = Button(self, text = "I do nothing!")
self.bttn1.grid()
# create second button
self.bttn2 = Button(self)
self.bttn2.grid()
self.bttn2.configure(text = "Me too!")
# create third button
self.bttn3 = Button(self)
self.bttn3.grid()
self.bttn3["text"] = "Same here!"
# main
root = Tk()
root.title("Lazy Buttons 2")
root.geometry("200x85")
app = Application(root)
root.mainloop()
Комментарии:
1. Спасибо, проблема была в том, что у меня версии 2.7 и 3.5)