2000815
@2000815 (Keishi Ichida)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

tkinterのスクロール方法

tkinterのスクロール方法

pythonのtkinterとBeautifulSoupライブラリーについて質問です。

現状ではエラー文は発生していませんが、思うような実装ができていない状況です。

該当するソースコード

import tkinter as tk 
import requests
from bs4 import BeautifulSoup

room =  tk.Tk()
room.title("スクレイピング") 
room.geometry("1000x1500")

canvas = tk.Canvas(room)
bar = tk.Scrollbar(room, orient=tk.VERTICAL)
bar.pack(side=tk.RIGHT, fill=tk.Y)
bar.config(command=canvas.yview) 
canvas.config(yscrollcommand=bar.set) 
canvas.config(scrollregion=(0,0,1000,1500)) 


frame = tk.Frame(canvas)
canvas.create_window((0,0), window=frame, anchor=tk.NW, width=1000, height=1500)
xtbox = tk.Text( width=2500, height=2500)

apple_ranking_html = requests.get("https://music.apple.com/jp/playlist/%E3%83%88%E3%83%83%E3%83%97100-%E6%97%A5%E6%9C%AC/pl.043a2c9876114d95a4659988497567be")

soup = BeautifulSoup(apple_ranking_html.text, "html.parser")
div = soup.div(class_="song-name typography-body-tall")
for d in div:
    divs = d.getText()
    static = tk.Label(text = divs)
    static.pack()

frame.pack()
room.mainloop()  

実行した写真を載せさせていただきます。

image.png
スクリーンショット 2021-04-10 7.04.10.png

これはApple Musicのtop100ランキングをスクレイピングしてそれをtkinterで表示しようとしています。
ですが、スクロールが機能していません。
スクロールバーの表示はできていますがうごかしても肝心の中身が動かずに困っています。
自分なりに調べてみてもスクロール範囲の指定できていないのではないかという結論を出してしますが、反映されません。

わかる方は回答よろしくお願いいたします。

0

2Answer

Comments

  1. @2000815

    Questioner

    ありがとうございます。参考にさせていただきます。

こんな感じでどうでしょうか?

  import tkinter as tk 
  import requests
  from bs4 import BeautifulSoup

  room =  tk.Tk()
  room.title("スクレイピング") 
  room.geometry("1000x1500")

- canvas = tk.Canvas(room)
+ canvas = tk.Canvas(room, width=1000, height=1500)
  bar = tk.Scrollbar(room, orient=tk.VERTICAL)
  bar.pack(side=tk.RIGHT, fill=tk.Y)
  bar.config(command=canvas.yview) 
  canvas.config(yscrollcommand=bar.set) 
  canvas.config(scrollregion=(0,0,1000,1500)) 


  frame = tk.Frame(canvas)
  canvas.create_window((0,0), window=frame, anchor=tk.NW, width=1000, height=1500)
  xtbox = tk.Text( width=2500, height=2500)

  apple_ranking_html = 
  requests.get("https://music.apple.com/jp/playlist/%E3%83%88%E3%83%83%E3%83%97100-%E6%97%A5%E6%9C%AC/pl.043a2c9876114d95a4659988497567be")

  soup = BeautifulSoup(apple_ranking_html.text, "html.parser")
  div = soup.div(class_="song-name typography-body-tall")
  vfor d in div:
      divs = d.getText()
-     static = tk.Label(text = divs)
+     static = tk.Label(frame, text = divs)
      static.pack()

- frame.pack()
+ canvas.pack()
  room.mainloop()  
1Like

Comments

  1. @2000815

    Questioner

    参考にさせていただきました。スクロールの動きをつけることができました。
    ありがとうございました。

Your answer might help someone💌