Python 在创建GUI时遇到问题
我正在做计算机科学作业,但遇到了问题。任务是创建一个带有按钮的gui,该按钮打开一个新窗口并显示来自Swapi的信息(可在www.Swapi.co上找到)我的问题是,它说我缺少位置参数,无法找出它们缺少的地方,或者我使用tkinter模块为Gui做了什么错误,我还导入了一个我创建的类。程序和类都位于下面Python 在创建GUI时遇到问题,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在做计算机科学作业,但遇到了问题。任务是创建一个带有按钮的gui,该按钮打开一个新窗口并显示来自Swapi的信息(可在www.Swapi.co上找到)我的问题是,它说我缺少位置参数,无法找出它们缺少的地方,或者我使用tkinter模块为Gui做了什么错误,我还导入了一个我创建的类。程序和类都位于下面 from tkinter import * from People import Person def FormatToString(collection): results
from tkinter import *
from People import Person
def FormatToString(collection):
results = ""
for item in collection:
response = requests.get(item)
dataRow = response.json()
results += " {}, ".format(dataRow['name'])
return results
def GetHomeWorld(homeworld):
response2 = requests.get(homeworld)
Json_data2 = response2.json()
homeworld = Json_data2['name']
return homeworld
root = Tk()
root.title('A visual Dictionary of the Star Wars Universe')
root.geometry('{}x{}'.format(460, 350))
# layout all of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
# create all of the main containers
top_frame = Frame(root, bg='cyan',padx=100, pady=8)
center = Frame(root, bg='gray', padx=3, pady=3)
top_frame.grid(row=0, sticky="ew")
center.grid(row=1, sticky="nsew")
# create the widgets for the top frame
model_label = Label(top_frame, text='Filter Example')
width_label = Label(top_frame, text='Filter by keyword:')
entry_W = Entry(top_frame, background="pink")
def DataFilter():
root.title(entry_W.get())
center.destroy()
button = Button(top_frame, text="Submit", command=DataFilter)
# layout the widgets in the top frame
model_label.grid(row=0, columnspan=3)
width_label.grid(row=1, column=0)
entry_W.grid(row=1, column=1)
button.grid(row=1, column=2)
import json
import requests
starAPI = "https://swapi.co/api/people/"
response = requests.get(starAPI)
json_data = response.json()
i = 1
Label(center, text= 'StarShips\n=========', width=20, pady=13, padx=13).grid(row=1, column = 0)
Label(center, text= 'Name\n=====', width=20, pady=13, padx=13).grid(row=1, column = 1)
Label(center, text= 'Birth Year\n==========', width=20, pady=13, padx=13).grid(row=1, column = 2)
Label(center, text= 'Gender\n======', width=10, pady=13, padx=13).grid(row=1, column = 3)
Label(center, text= 'Eye Color\n=========', width=15, pady=13, padx=13).grid(row=1, column = 4)
Label(center, text= 'Homeworld\n========', width=15, pady=13, padx=13).grid(row=1, column = 5)
for person in (json_data['results']):
name = str(person['name'])
birth_year = str(person['birth_year'])
gender = str(person['gender'])
eye_color = str(person['eye_color'])
homeworld = GetHomeWorld(person['homeworld'])
starships = FormatToString(person['starships'])
i += 1
Button(center, text = 'Click Me for StarShips', width = 20, pady = 13, padx = 13, bg = 'black', fg = 'white', command = Person.openwindow(starships)).grid(row=i, column = 0)
Label(center, text= name, width=20, pady=13, padx=13).grid(row=i, column = 1)
Label(center, text= birth_year, width=20, pady=13, padx=13).grid(row=i, column = 2)
Label(center, text= gender, width=10, pady=13, padx=13).grid(row=i, column = 3)
Label(center, text= eye_color, width=15, pady=13, padx=13).grid(row=i, column = 4)
Label(center, text= homeworld, width=15, pady=13, padx=13).grid(row=i, column = 5
root.mainloop()
=====================================================================================================
import json
import requests
from tkinter import *
class Person():
def __init__(self, name, birth_year, eye_color, gender, homeworld, starships):
self.name = name
self.birth_year = birth_year
self.eye_color = eye_color
self.gender = gender
self.starships = self.FormatToString(starships)
self.homeworld = self.GetHomeWorld(homeworld)
def FormatToString(self, collection):
results = ""
for item in collection:
response = requests.get(item)
dataRow = response.json()
results += " {} |".format(dataRow['name'])
return results
def GetHomeWorld(self, homeworld):
response2 = requests.get(homeworld)
Json_data2 = response2.json()
homeworld = Json_data2['name']
return homeworld
def openwindow(self, starships):
window = Tk()
window.geometry('{}x{}'.format(460, 350))
window.title('Starships')
# layout all of the secondary containers
window.grid_rowconfigure(1, weight=1)
window.grid_columnconfigure(0, weight=1)
# create all of the secondary containers
top_frame2 = Frame(window, bg='cyan',padx=100, pady=8)
center2 = Frame(window, bg='gray', padx=3, pady=3)
top_frame2.grid(row=0, sticky="ew")
center2.grid(row=1, sticky="nsew")
Label(center2, text= starships, width=30, pady=13, padx=13).grid(row=0, column = 0)
window.mainloop
def __str__(self):
return "Name: {}\nBirth year: {}\nGender: {}\nEye Color: {}\nStarships: {}\nHomeworld: {}".format(self.name, self.birth_year, self.gender, self.eye_color, self.starships, self.homeworld)
如果您使用
Person.openstarship(starship)
,则将其作为静态方法运行,而不是类方法,因此它不使用self
,并通知您缺少值
您应该使用decorator@staticmethod
并删除self
@staticmethod
def openwindow(starships):
或者您必须创建类Person
person_instance = Person(...put values...)
Button(..., command=person_instance.openwindow(starship))
但是command=
还有其他错误。它需要不带()
和参数的函数名,之后它将使用()来运行此函数。要使用参数指定方法,可以使用
lambda`
Button(..., command=lambda:person_instance.openwindow(starship))
但在循环中创建的command=
中使用参数需要使用新变量从原始变量复制访问权限,而原始变量将在下一个循环中被覆盖
Button( command=(lambda x=starships:Person.openwindow(x)) )
其他问题:GUI应该只有一个使用Tk()
创建的主窗口。要创建其他窗口,请使用Toplevel()
。在其他GUI框架中也可以类似
所有GUI框架都使用主循环(也称为事件循环
),并且在所有GUI框架中应该只有一个主循环-在tkinter
中,它是tkinter.mainloop()
还有其他建议,但不是错误:
import*
不是首选,最好使用import tkinter
或非常流行的import tkinter as tk
。然后你必须使用tk.tk()
,tk.Button()
,等等
- 方法/函数应具有
小写\u名称
-ie.get\u home\u world()
格式化为字符串
您可以在中看到的所有建议
请分享错误始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是截图)进行讨论(不是评论)。还有其他有用的信息。您应该只使用Tk()
来创建主窗口。对于其他窗口,请使用Toplevel()
阅读如果您使用Person.openstarship(starship)
,则它应该是使用decorator@staticmethod
创建的静态方法,而不使用def openstarship(starship)
中的self
。它给了你缺少的论点。
import json
import requests
from tkinter import *
class Person():
def __init__(self, name, birth_year, eye_color, gender, homeworld, starships):
self.name = name
self.birth_year = birth_year
self.eye_color = eye_color
self.gender = gender
self.starships = self.FormatToString(starships)
self.homeworld = self.GetHomeWorld(homeworld)
def FormatToString(self, collection):
results = ""
for item in collection:
response = requests.get(item)
dataRow = response.json()
results += " {} |".format(dataRow['name'])
return results
def GetHomeWorld(self, homeworld):
response2 = requests.get(homeworld)
Json_data2 = response2.json()
homeworld = Json_data2['name']
return homeworld
@staticmethod
def openwindow(starships):
window = Toplevel()
#window.geometry('460x350')
window.title('Starships')
# layout all of the secondary containers
window.grid_rowconfigure(1, weight=1)
window.grid_columnconfigure(0, weight=1)
# create all of the secondary containers
top_frame2 = Frame(window, bg='cyan',padx=100, pady=8)
center2 = Frame(window, bg='gray', padx=3, pady=3)
top_frame2.grid(row=0, sticky="ew")
center2.grid(row=1, sticky="nsew")
Label(center2, text= starships, width=30, pady=13, padx=13).grid(row=0, column = 0)
#window.mainloop()
def __str__(self):
return "Name: {}\nBirth year: {}\nGender: {}\nEye Color: {}\nStarships: {}\nHomeworld: {}".format(self.name, self.birth_year, self.gender, self.eye_color, self.starships, self.homeworld)
from tkinter import *
def FormatToString(collection):
results = ""
for item in collection:
response = requests.get(item)
dataRow = response.json()
results += " {}, ".format(dataRow['name'])
return results
def GetHomeWorld(homeworld):
response2 = requests.get(homeworld)
Json_data2 = response2.json()
homeworld = Json_data2['name']
return homeworld
root = Tk()
root.title('A visual Dictionary of the Star Wars Universe')
#root.geometry('460x350')
# layout all of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
# create all of the main containers
top_frame = Frame(root, bg='cyan',padx=100, pady=8)
center = Frame(root, bg='gray', padx=3, pady=3)
top_frame.grid(row=0, sticky="ew")
center.grid(row=1, sticky="nsew")
# create the widgets for the top frame
model_label = Label(top_frame, text='Filter Example')
width_label = Label(top_frame, text='Filter by keyword:')
entry_W = Entry(top_frame, background="pink")
def DataFilter():
root.title(entry_W.get())
center.destroy()
button = Button(top_frame, text="Submit", command=DataFilter)
# layout the widgets in the top frame
model_label.grid(row=0, columnspan=3)
width_label.grid(row=1, column=0)
entry_W.grid(row=1, column=1)
button.grid(row=1, column=2)
import json
import requests
starAPI = "https://swapi.co/api/people/"
response = requests.get(starAPI)
json_data = response.json()
print(json_data)
i = 1
Label(center, text= 'StarShips\n=========', width=20, pady=13, padx=13).grid(row=1, column = 0)
Label(center, text= 'Name\n=====', width=20, pady=13, padx=13).grid(row=1, column = 1)
Label(center, text= 'Birth Year\n==========', width=20, pady=13, padx=13).grid(row=1, column = 2)
Label(center, text= 'Gender\n======', width=10, pady=13, padx=13).grid(row=1, column = 3)
Label(center, text= 'Eye Color\n=========', width=15, pady=13, padx=13).grid(row=1, column = 4)
Label(center, text= 'Homeworld\n========', width=15, pady=13, padx=13).grid(row=1, column = 5)
for person in (json_data['results']):
name = str(person['name'])
birth_year = str(person['birth_year'])
gender = str(person['gender'])
eye_color = str(person['eye_color'])
homeworld = GetHomeWorld(person['homeworld'])
starships = FormatToString(person['starships'])
i += 1
Button(center, text = 'Click Me for StarShips', width = 20, pady = 13, padx = 13, bg = 'black', fg = 'white', command=lambda x=starships:Person.openwindow(x)).grid(row=i, column = 0)
Label(center, text= name, width=20, pady=13, padx=13).grid(row=i, column = 1)
Label(center, text= birth_year, width=20, pady=13, padx=13).grid(row=i, column = 2)
Label(center, text= gender, width=10, pady=13, padx=13).grid(row=i, column = 3)
Label(center, text= eye_color, width=15, pady=13, padx=13).grid(row=i, column = 4)
Label(center, text= homeworld, width=15, pady=13, padx=13).grid(row=i, column = 5)
root.mainloop()