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

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

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

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

Tkinter

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

3回答

1256閲覧

tkinterのサブウィンドのボタンの位置が変えられない

momey0709

総合スコア5

Python 3.x

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

Tkinter

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

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/09/09 18:43

pythonのtkinterを使用してカレンダーアプリを作成しているんですが、
サブウィンドウにて表示しているボタンの位置を画面右下のすみにしたいのですが、
ボタンの位置が変えられません。
pack、gridのオプションをいろいろ試しましたが、
デフォルトの中央表示のまま変わりませんでした。
placeにするとそもそもボタンが表示されなくなってしまいます。

何か方法はないでしょうか?
ご教授の程、お願い致します。

python

1# -*- coding:utf-8 -*- 2 3import tkinter as tk 4 5 6# カレンダーを作成するフレームクラス 7class mycalendar(tk.Frame): 8 def __init__(self, master=None, cnf={}, **kw): 9 "初期化メソッド" 10 import datetime 11 tk.Frame.__init__(self, master, cnf, **kw) 12 13 # 現在の日付を取得 14 now = datetime.datetime.now() 15 # 現在の年と月を属性に追加 16 self.year = now.year 17 self.month = now.month 18 19 # frame_top部分の作成 20 frame_top = tk.Frame(self) 21 frame_top.pack(pady=5) 22 self.previous_month = tk.Label(frame_top, text="<", font=("", 14)) 23 self.previous_month.bind("<1>", self.change_month) 24 self.previous_month.pack(side="left", padx=10) 25 self.current_year = tk.Label(frame_top, text=self.year, font=("", 18)) 26 self.current_year.pack(side="left") 27 self.current_month = tk.Label(frame_top, text=self.month, font=("", 18)) 28 self.current_month.pack(side="left") 29 self.next_month = tk.Label(frame_top, text=">", font=("", 14)) 30 self.next_month.bind("<1>", self.change_month) 31 self.next_month.pack(side="left", padx=10) 32 33 # frame_week部分の作成 34 frame_week = tk.Frame(self) 35 frame_week.pack() 36 button_mon = d_button(frame_week, text="Mon") 37 button_mon.grid(column=0, row=0) 38 button_tue = d_button(frame_week, text="Tue") 39 button_tue.grid(column=1, row=0) 40 button_wed = d_button(frame_week, text="Wed") 41 button_wed.grid(column=2, row=0) 42 button_thu = d_button(frame_week, text="Thu") 43 button_thu.grid(column=3, row=0) 44 button_fri = d_button(frame_week, text="Fri") 45 button_fri.grid(column=4, row=0) 46 button_sta = d_button(frame_week, text="Sat", fg="blue") 47 button_sta.grid(column=5, row=0) 48 button_san = d_button(frame_week, text="San", fg="red") 49 button_san.grid(column=6, row=0) 50 51 # frame_calendar部分の作成 52 self.frame_calendar = tk.Frame(self) 53 self.frame_calendar.pack() 54 55 # 日付部分を作成するメソッドの呼び出し 56 self.create_calendar(self.year, self.month) 57 58 def create_calendar(self, year, month): 59 "指定した年(year),月(month)のカレンダーウィジェットを作成する" 60 61 # ボタンがある場合には削除する(初期化) 62 try: 63 for key, item in self.day.items(): 64 item.destroy() 65 except: 66 pass 67 68 # calendarモジュールのインスタンスを作成 69 import calendar 70 cal = calendar.Calendar() 71 # 指定した年月のカレンダーをリストで返す 72 days = cal.monthdayscalendar(year, month) 73 74 # 日付ボタンを格納する変数をdict型で作成 75 self.day = {} 76 # for文を用いて、日付ボタンを生成 77 for i in range(0, 42): 78 c = i - (7 * int(i / 7)) 79 r = int(i / 7) 80 try: 81 # 日付が0でなかったら、ボタン作成 82 if days[r][c] != 0: 83 self.day[i] = d_button(self.frame_calendar, text=days[r][c], command=self.createNewWindow) 84 self.day[i].grid(column=c, row=r) 85 except: 86 """ 87 月によっては、i=41まで日付がないため、日付がないiのエラー回避が必要 88 """ 89 break 90 91 def change_month(self, event): 92 # 押されたラベルを判定し、月の計算 93 if event.widget["text"] == "<": 94 self.month -= 1 95 else: 96 self.month += 1 97 # 月が0、13になったときの処理 98 if self.month == 0: 99 self.year -= 1 100 self.month = 12 101 elif self.month == 13: 102 self.year += 1 103 self.month = 1 104 # frame_topにある年と月のラベルを変更する 105 self.current_year["text"] = self.year 106 self.current_month["text"] = self.month 107 # 日付部分を作成するメソッドの呼び出し 108 self.create_calendar(self.year, self.month) 109 110 def createNewWindow(self): 111 self.newWindow = tk.Toplevel(root) 112 self.app = Win2(self.newWindow) 113 114 115# デフォルトのボタンクラス 116class d_button(tk.Button): 117 def __init__(self, master=None, cnf={}, **kw): 118 tk.Button.__init__(self, master, cnf, **kw) 119 self.configure(font=("", 14), height=2, width=4, relief="flat") 120 121 122class Win2(tk.Frame): 123 def __init__(self,master): 124 125 super().__init__(master) 126 self.pack() 127 self.master.geometry("500x500") 128 self.master.title("add schedule") 129 self.create_widgets() 130 131 def create_widgets(self): 132 # Button 133 self.button_quit = tk.Button(self,text="Close add schedule", command=self.quit_window) 134 self.button_quit.pack() 135 136 def quit_window(self): 137 self.master.destroy() 138 139 140# ルートフレームの定義 141root = tk.Tk() 142root.title("Calendar App") 143mycal = mycalendar(root) 144mycal.pack() 145root.mainloop()

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

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

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

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

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

guest

回答3

0

サブウィンドウのボタンはサブウィンドウに配置しないといけないので、
配置オブジェクトをselfではなくサブウィンドウを作成するToplevelを代入した
self.masterを指定してください。

python

1 def create_widgets(self): 2 # Button 3 self.button_quit = tk.Button(self.master,text="Close add schedule", command=self.quit_window) 4 self.button_quit.place(x=350, y=450)

これでplaceが効くようになります。
ちなみにpackにanchor=tk.SE等を指定しても、相対配置なので右には寄りますが、
別にウィジェットを配置しないと上詰めのままです。

投稿2020/09/10 01:22

yureighost

総合スコア2183

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

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

0

ベストアンサー

yureighostさんの回答に併せて補足という形で。
self.button_quitの配置は123行目で定義しているself.masterに配置するという事と
packオプションを使用してウィンドウ右下に配置したいという場合には
anchort=tk.NEに併せてside=tk.BOTTOMで要素を下に詰める事が出来るので右下に配置出来る様になります。

python

1 def create_widgets(self): 2 # Button 3 self.button_quit = tk.Button(self.master,text="Close add schedule", command=self.quit_window) 4 self.button_quit.pack(anchor=tk.NE, side=tk.BOTTOM)

投稿2020/09/10 04:05

編集2020/09/10 04:10
nto

総合スコア1438

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

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

0

邪道っぽいやり方かもしれませんが、Win2クラスの__init__にあるself.pack()をself.placeに変えたらボタン位置の調整ができました。

class Win2(tk.Frame): def __init__(self,master): super().__init__(master) self.place(x=350,y=450) #self.pack() self.master.geometry("500x500") self.master.title("add schedule") self.create_widgets() def create_widgets(self): # Button self.button_quit = tk.Button(self,text="Close add schedule", command=self.quit_window) self.button_quit.pack() def quit_window(self): self.master.destroy()

投稿2020/09/10 01:15

amahara_waya

総合スコア1029

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問