目前我放完他不會左右再下放一格
import os
import tkinter as tk
from PIL import Image, ImageTk
from tkinter.constants import CENTER
from tkinter.simpledialog import askstring
import tkinter.messagebox
class MyCanvas(tk.Canvas):
print('hi')
master = tk.Tk()
settime = tk.Button(master,text="設定警報時間",width=10,height=3,font=('Arial', '15'))
stop_alarm = tk.Button(master,text="停上警報",width=10,height=3,font=('Arial', '15'))
imgpath = '/home/asc/python_code/map.png'
img = Image.open(imgpath)
w,h = img.size
master.minsize(1080, 800)
# master maximum window size value
master.maxsize(1080, 800)
while w<1080 or h<720:
w = int(w*1.05)
h = int(h*1.05)
while w>1080 or h>720:
w = int(w//1.1)
h = int(h//1.1)
img =img.resize((w, h))
settime.pack(side="left",fill='x',anchor = 'w')
stop_alarm.pack(side="right",fill='x',anchor = 'w')
photo = ImageTk.PhotoImage(img)
w = MyCanvas(master)
w.create_image(0, 0, image=photo, anchor="nw")
w.pack(side="bottom",fill=tk.BOTH,expand=1)
w.create_oval(80.4,80,50,50,fill='red')
tk.mainloop()
要怎麼做才能到排成這樣子?
https://ibb.co/2F7T5wC
給你另一個參考方向(使用grid)
我自己認為會比較好排版(我比較常用這個)
但你可能要先設計一個佈局圖,像是你給的網頁那種
img =img.resize((w, h))
#settime.pack(side="left",fill='x',anchor = 'w')
#stop_alarm.pack(side="right",fill='x',anchor = 'w')
settime.grid(row=0, column=0)
stop_alarm.grid(row=0, column=1)
photo = ImageTk.PhotoImage(img)
w = MyCanvas(master)
w.create_image(0, 30, image=photo, anchor="w")
#w.pack(side="bottom",fill=tk.BOTH,expand=1)
w.grid(row=2, column=0, columnspan=2)
w.create_oval(80.4,80,50,50,fill='red')
簡易的說明一下,就是每個元件都要設定一個擺位給他(row,column的定位)
columnspan=2 #就有點類似合併儲存格的概念,參數2就是佔據2格(欄,左右)
rowspan=3 #參數3就是佔據3格(列,上下)
此外邊框問題,可以藉由修改sticky的參數去修正,這部分你可以問一下google~
是有那種形了,可是請問怎樣做才能做到buttom在grid的情況下做到像pack樣吃到X軸最滿,以及下方的圖也吃到最滿版大小?
在你設定視窗大小的地方加上網格的個數
#container.columnconfigure(index, weight)
#container.rowconfigure(index, weight)
#2*2
master.columnconfigure(0, weight=2)
master.columnconfigure(1, weight=2)
master.rowconfigure(0, weight=2)
master.rowconfigure(1, weight=2)
master.minsize(1080, 800)
# master maximum window size value
master.maxsize(1080, 800)
下面調整一下
img =img.resize((w, h))
#settime.pack(side="left",fill='x',anchor = 'w')
#stop_alarm.pack(side="right",fill='x',anchor = 'w')
settime.grid(row=0, column=0,sticky=tk.W+tk.E+tk.N)
stop_alarm.grid(row=0, column=1,sticky=tk.W+tk.E+tk.N)
photo = ImageTk.PhotoImage(img)
w = MyCanvas(master)
w.create_image(0, 0, image=photo, anchor="nw")
#w.pack(side="bottom",fill=tk.BOTH,expand=1)
w.grid(row=1, column=0, sticky=tk.W+tk.E+tk.N+tk.S,columnspan=2)
謝謝前輩的詳細的講解,大概有看懂是要吃TK 裡的 W、E、N、S坐標。
https://ibb.co/gDfn2hr
不過create_image 那邊是出現什麼問題? 我用pack 都會吃全圖,但換grid沒有放完整一張
w.create_image(0, 0, image=photo, anchor="nw")
調整anchor看看?
我試過nw ne ns 都會出現這問題
https://ibb.co/q0HN770
中明有一塊好像怎樣都會吃掉(紅色圈)