首页 > 解决方案 > 对将提取 html 代码并从网站显示它的 tkinter GUI 进行故障排除?

问题描述

l 必须开发一个交互式程序,让用户从几个排序的 HTML 列表中进行选择,并使用小部件挑选某些 HTML 代码以显示每个列表中的当前第二位置。每个小部件每次也显示来自不同 HTML 网站的不同列表。我已经设置了所有小部件和 GUI,但我不确定如何完成每个小部件提取 HTML 代码并将其显示在窗口中的任务。

我遇到的主要问题是,从实时网站中提取 HTML 代码并使用我的单选按钮小部件显示它。希望有人能指出我正确的方向。

这是我到目前为止完成的代码


root = Tk()
root.title("Runners Up")
root.config(background = "#f0f0f0")

test_status0 = BooleanVar()
test_status1 = BooleanVar()
test_status2 = BooleanVar()
test_status3 = BooleanVar()
test_status4 = BooleanVar()
 

#Label Frame for Radio Buttons
radiobuttonsframe = LabelFrame(root, relief = 'groove', font = 'Arial',
                              borderwidth = 5)
radiobuttonsframe.grid(row=0, column=1, padx=10, pady=2)


#Picture Label Frame
PicFrame = LabelFrame(root, relief = 'groove', borderwidth = 5)
PicFrame.grid(row=0, column=0, padx=10, pady=2)


#Display Frame (Runner Up)
RunnerFrame = LabelFrame(root, relief = 'groove', borderwidth = 5, font = 'Arial',
                         text = 'Runner Up')
RunnerFrame.grid(row=1, column=0, padx=10, pady=2)


#Display Frame (Full List)
ListFrame = LabelFrame(root, relief = 'groove', borderwidth = 5, font = 'Arial',
                       text = 'Full List')
ListFrame.grid(row=1, column=1, padx=10, pady=2)


#Importing Photo
Picture1 = PhotoImage(file="Secondplace.gif")
Label(PicFrame, image=Picture1).grid(row=0, column=0, padx=10, pady=2)



#Frames for each widget
btnFrame = LabelFrame(radiobuttonsframe, relief = 'groove',
                      font = 'Arial', borderwidth = 2, text = 'Current Runner Ups', background="#f0f0f0")
btnFrame.grid(row=1, column=0, padx=10, pady=2)

btnFramePrev = LabelFrame(radiobuttonsframe, relief = 'groove',
                      font = 'Arial', borderwidth = 2, text = 'Previous Runner Ups', background="#f0f0f0")
btnFramePrev.grid(row=2, column=0, padx=10, pady=50)

updaterBtn = LabelFrame(radiobuttonsframe, relief = 'groove',
                      font = 'Arial', borderwidth = 2, background="#f0f0f0")
updaterBtn.grid(row=3, column=0, padx=10, pady=2)


#Button Music Runner ups
MusicButton = Radiobutton(btnFrame, text="Top Album Charts [Title and Artist]",
                     height=2, width=50, value = test_status1, command = test1)
MusicButton.grid(row=0, column=0, padx=10, pady=2)

#Button for Movie Runner ups
MovieButton = Radiobutton(btnFrame, text="Movie Charts [Title and Production]",
                     height=2, width=50, value = test_status2, command = test1)
MovieButton.grid(row=1, column=0, padx=10, pady=2)

#Button for games runner ups
GamesButton = Radiobutton(btnFrame, text="Game Charts [Title and Players]",
                     height=2, width=50, value = test_status3, command = test1)
GamesButton.grid(row=2, column=0, padx=10, pady=2)

#Button for Medal runner ups
MedalButton = Radiobutton(btnFramePrev, text="Medal 2001 [Name and Votes]",
                   height=2, width=50, value = test_status4, command = test1)
MedalButton.grid(row=3, column=0, padx=10, pady=2)


#Scroll Bar for full list of winners
ListScroll = ScrolledText(ListFrame, width=40, height=10)
ListScroll.grid(row=0, column=0, padx=10, pady=2)

#Text Box for our main Runners Up
RunnersText = Text(RunnerFrame, width=50, height=10)
RunnersText.grid(row=0, column=0, padx=10, pady=2)

#Update and Show Source Buttons
Update = Button(updaterBtn, text="Update", height=2, width=10)
Update.grid(row=0, column=0, padx=10, pady=2)
ShowSource = Button(updaterBtn, text="Show Source", height=2, width=10)
ShowSource.grid(row=0, column=1, padx=10, pady=2)
root.mainloop() 

\EDIT// 我已经想出运行按钮,但现在我实际上无法让 HTML 文本显示在我的文本框中。

这是我尝试的尝试:

def MusicRunner():
    RunnersText.delete(0.0, END)
    site = 'Official Charts'
    feed_web = download('https://www.officialcharts.com/charts/singles-chart/')
    items = findall(r'<body class="sticky-nav" style \b[^>]*>(.*?)<\/body>', feed_web, flags=MULTILINE|DOTALL)
    num = findall(r'<span class= "position">"2"</span>',feed_web, flags=MULTILINE|DOTALL)   
    RunnersText.insert(INSERT, num)

标签: pythonhtmluser-interfacetkinter

解决方案


推荐阅读