python - Python tkinter 将值传递给另一个类(似乎只接受一个值)
问题描述
我正在尝试制作一个 tkinter GUI,它将抓取网站的某些部分,以使其对用户友好,我制作它以便它在 tkinter 中具有不同的框架。问题是,在帧之间切换时,我似乎无法传递其他信息。这是我的代码(https://codeshare.io/aYXv6Y):
import tkinter as tk # python 3
from tkinter import font as tkfont # python 3
from bs4 import BeautifulSoup
import requests
import time
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.geometry("650x350")
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
self.title_font2 = tkfont.Font(family='Calibri', size=11)
self.title_fontmainTMenu = tkfont.Font(family='Helvetica', size=16)
self.title_fontDevices = tkfont.Font(family='Didot', size=13)
self.title_fontT = tkfont.Font(family='Cambria', size=16)
self.title("League of Legends Helper")
changeChampX = 525
changeChampY = 310
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (MainMenu, ChampionSelect, ChampionRunes):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("MainMenu")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
def setChampion(self, championName, championRole):
self.championName = championName
self.championRole = championRole
class MainMenu(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Welcome to the Main Menu", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Champion Select",
command=lambda: controller.show_frame("ChampionSelect"))
button1.pack()
class ChampionSelect(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Which Champion are you playing?", font=controller.title_fontmainTMenu)
label.place(x=170, y=70)
nameOfChampionE = tk.Entry(self)
nameOfChampionE.place(x=170, y=150, width=120, height = 20)
roleOfChampionE = tk.Entry(self)
roleOfChampionE.place(x=170, y=250, width=120, height=20)
page = requests.get('https://champion.gg/champion/Katarina/Middle?')
soup = BeautifulSoup(page.text, 'lxml')
championData = soup.find('div', 'summoner-text')
def whichChampion():
championName = nameOfChampionE.get()
championRole = roleOfChampionE.get()
controller.setChampion(championName, championRole)
command = controller.show_frame("ChampionRunes")
submitName = tk.Button(self, text="Show Champion details",
command=lambda: whichChampion())
submitName.place(x=170, y=185)
class ChampionRunes(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Change Champion",
command=lambda: controller.show_frame("ChampionSelect"))
button.place(x=525, y=310)
# URL = f"https://champion.gg/champion/{controller.championName.capitalize()}/{controller.championRole.capitalize()}?"
text = controller.ChampionName
titleChamp = tk.Label(self, text=f"{text}", font=controller.title_fontmainTMenu)
titleChamp.place(x=170, y=70)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
尝试传递 ChampionName 和 ChampionRole 时,出现错误:
Traceback (most recent call last):
File "(FILE PATH)", line 130, in <module>
app = SampleApp()
File "(FILE PATH)", line 34, in __init__
frame = F(parent=container, controller=self)
File "(FILE PATH)", line 119, in __init__
text = controller.ChampionName
File "(FILE PATH)", line 2345, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'ChampionName'
ChampionName 和 ChampionRole 是我正在解析的网站的 URL 的一部分的关键信息,所以我真的不知道该怎么做。我对代码的某些部分不熟悉,并且对 python 和 tkinter 还是新手,所以请尽量清楚。
任何帮助将非常感激。谢谢!
解决方案
推荐阅读
- php - DOMXPath 不使用有效的表达式
- reactjs - 如何将 totalPrice 传递给 taxSum.js?
- node.js - 如何使用 Fetch 在前端获取后端错误消息
- python - Beautifulsoup findAll, how to get the second text
- python - 最简单的写法“虽然不是初始值”
- angular - Angular 7 组件测试 - 如何验证输入元素值
- python - python类中的条件
- cmake - 以下配置文件被考虑但未被接受:
- serialization - 如何使用需要实现者实现 serde::Deserialize 的方法创建特征
- javascript - 如何在 jquery 语法函数中发送参数?