@nanachandanyan

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

tkinterのwindow拡大したときの処理(python)

Q&A

Closed

tkinterのwindow拡大したときの処理(python)

frame1:ボタン等
frame2:テキスト枠
としたときに、window拡大時、テキスト枠だけ拡大するように設定したいです。
テキスト枠上側を移動させないようにするにはどうしたらよいでしょうか。

現状のコード

from tkinter import *
from tkinter import ttk
from tkinter.font import Font

root = Tk()
root.title("test")
root.minsize(100, 100)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

def btn1():
    root.destroy()
def btn2():
    root.destroy()

# ボタン用
frame1 = ttk.Frame(root, padding=10)
frame1.rowconfigure(1, weight=0)
frame1.columnconfigure(0, weight=0)
frame1.grid(sticky=(N, W, S, E))

# テキスト入力・スクロールバー用
frame2 = ttk.Frame(root, padding=10)
frame2.rowconfigure(1, weight=1)
frame2.columnconfigure(0, weight=1)
frame2.grid(sticky=(N, W, S, E))

# Button
button1 = ttk.Button(
    frame1, text="ボタン1", command=btn1)
button1.grid(
    row=0, column=0, columnspan=2, sticky=(N, W))

button2 = ttk.Button(
    frame1, text="ボタン2", command=btn2)
button2.grid(
    row=1, column=0, columnspan=2, sticky=(N, W))

# Text
f = Font(family='Helvetica', size=16)
v1 = StringVar()
txt = Text(frame2, height=15, width=70)
txt.configure(font=f)
txt.insert(1.0, "テキスト")
txt.grid(row=3, column=0, sticky=(N, W, S, E))

# Scrollbar
scrollbar = ttk.Scrollbar(
    frame2,
    orient=VERTICAL,
    command=txt.yview)
txt['yscrollcommand'] = scrollbar.set
scrollbar.grid(row=3, column=1, sticky=(N, S))

windowを引っ張って拡大した際に、以下矢印部分が開いてしまいます。
要望としては、矢印部分が動かず、テキスト枠とスクロールバーのみがwindow拡大に追従し広がるようにしたい次第です。
image.png

0 likes

1Answer

gridの引数のcolumn, rowは親フレームの中での位置を表します。

from tkinter import *
from tkinter import ttk
from tkinter.font import Font

root = Tk()
root.title("test")
root.minsize(100, 100)
root.columnconfigure(0, weight=1)
-root.rowconfigure(0, weight=1)
+root.rowconfigure(0, weight=0)
+root.rowconfigure(1, weight=1)

def btn1():
    root.destroy()
def btn2():
    root.destroy()

# ボタン用
frame1 = ttk.Frame(root, padding=10)
frame1.rowconfigure(1, weight=0)
frame1.columnconfigure(0, weight=0)
frame1.grid(sticky=(N, W, S, E))

# テキスト入力・スクロールバー用
frame2 = ttk.Frame(root, padding=10)
frame2.rowconfigure(1, weight=1)
frame2.columnconfigure(0, weight=1)
frame2.grid(sticky=(N, W, S, E))

# Button
button1 = ttk.Button(
    frame1, text="ボタン1", command=btn1)
button1.grid(
    row=0, column=0, columnspan=2, sticky=(N, W))

button2 = ttk.Button(
    frame1, text="ボタン2", command=btn2)
button2.grid(
    row=1, column=0, columnspan=2, sticky=(N, W))

# Text
f = Font(family='Helvetica', size=16)
v1 = StringVar()
txt = Text(frame2, height=15, width=70)
txt.configure(font=f)
txt.insert(1.0, "テキスト")
-txt.grid(row=3, column=0, sticky=(N, W, S, E))
+txt.grid(row=0, column=0, sticky=(N, W, S, E))

# Scrollbar
scrollbar = ttk.Scrollbar(
    frame2,
    orient=VERTICAL,
    command=txt.yview)
txt['yscrollcommand'] = scrollbar.set
-scrollbar.grid(row=3, column=1, sticky=(N, S))
+scrollbar.grid(row=0, column=1, sticky=(N, S))

1Like

Comments

  1. @nanachandanyan

    Questioner

    おおおおおおおおおおおおおおお
    素晴らしいです!
    ありがとうございます;w;

    初投稿で心配でしたが、何時間も悩んだものがさっと解決できてよかったです。

Your answer might help someone💌