質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Q&A

1回答

6855閲覧

python tkinterでフレームの中でスクロール?させたい

yahho

総合スコア28

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

0グッド

0クリップ

投稿2018/07/27 06:02

pythonのtkinterを使ってフレーム内でのスクロール?を可能にしたいです。

python

1root = Tk.Tk() 2root.title("tkinter") 3root.geometry("250x130") 4root.resizable(0,0) 5 6test_f = Tk.Frame(root, width = 245, height = 125) 7test_f.place(x = 1, y = 1) 8 9test_label = [1,2,3,4,5] 10y = 5 11 12for label in test_label: 13= Tk.Label(test_f,text=label, width = 14,bg='Red') 14.place(x=5, y=y) 15 y += 25 16 17root.mainloop()

このコードを書くと下のGUIが出来ます
イメージ説明

この状態でtest_labelの中の要素を6,7,8,9....と増やすと
labelは作られるけど見れない状態になってしまいす。

そこでrootのサイズは変えずにスクロールで確認できるようにしたいと考えています。
タイトルではフレームと書いていますがフレームでこの動作が出来るかは分からなかったので?にさせて頂きました。

ご存知の方いらっしゃいましたらご回答よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

例えば、こういうことでしょうかね

python3

1 2#ウィンドウのスクロールバーの作成 3 4from tkinter import * 5 6 7class VerticalScrolledFrame(Frame): 8 """A pure Tkinter scrollable frame that actually works! 9 * Use the 'interior' attribute to place widgets inside the scrollable frame 10 * Construct and pack/place/grid normally 11 * This frame only allows vertical scrolling 12 13 """ 14 def __init__(self, parent, *args, **kw): 15 Frame.__init__(self, parent, *args, **kw) 16 17 # スクロールバーの作成 18 vscrollbar = Scrollbar(self, orient=VERTICAL) 19 vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE) 20 canvas = Canvas(self, bd=0, highlightthickness=0, 21 yscrollcommand=vscrollbar.set) 22 canvas.pack(side=LEFT, fill=BOTH, expand=TRUE) 23 vscrollbar.config(command=canvas.yview) 24 25 # ビューをリセット 26 canvas.xview_moveto(0) 27 canvas.yview_moveto(0) 28 29 # create a frame inside the canvas which will be scrolled with it 30 self.interior = interior = Frame(canvas) 31 interior_id = canvas.create_window(0, 0, window=interior, 32 anchor=NW) 33 34 # track changes to the canvas and frame width and sync them, 35 # also updating the scrollbar 36 def _configure_interior(event): 37 # update the scrollbars to match the size of the inner frame 38 size = (interior.winfo_reqwidth(), interior.winfo_reqheight()) 39 canvas.config(scrollregion="0 0 %s %s" % size) 40 if interior.winfo_reqwidth() != canvas.winfo_width(): 41 # update the canvas's width to fit the inner frame 42 canvas.config(width=interior.winfo_reqwidth()) 43 interior.bind('<Configure>', _configure_interior) 44 45 def _configure_canvas(event): 46 if interior.winfo_reqwidth() != canvas.winfo_width(): 47 # update the inner frame's width to fill the canvas 48 canvas.itemconfigure(interior_id, width=canvas.winfo_width()) 49 canvas.bind('<Configure>', _configure_canvas) 50 51 52if __name__ == "__main__": 53 54 class SampleApp(Tk): 55 def __init__(self, *args, **kwargs): 56 root = Tk.__init__(self, *args, **kwargs) 57 58 59 self.frame = VerticalScrolledFrame(root) 60 self.frame.pack() 61 self.label = Label(text="ウィンドウを縮めてみてください。スクロールバーが出てきます.") 62 self.label.pack() 63 buttons = [] 64 for i in range(20): 65 buttons.append(Button(self.frame.interior, text="Button " + str(i))) 66 buttons[-1].pack() 67 68 app = SampleApp() 69 app.mainloop() 70

投稿2018/08/03 15:18

omoiyari.keita

総合スコア136

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問