2017-05-04 37 views
0

這是到目前爲止我的代碼:Tkinter的格式化窗口布局

from tkinter import * 
from tkinter import ttk 

#create root window 
root = Tk() 
root.geometry('640x480+25+75') 
root.title('Dragon Slayer') 

#create Main frame 
frame = ttk.Frame(root) 
frame.grid(sticky= N+W+E+S) 
frame.config(height = 480, width = 640) 
frame.config(relief = SUNKEN) 

#create Left Controls frame 
frame_controls = ttk.Frame(frame) 
frame_controls.grid(row = 1, column = 1, sticky="nesw") 
frame_controls.config(height = 480, width = 125) 
frame_controls.config(relief = SUNKEN) 
label = ttk.Label(frame_controls) 
label.grid(row = 1) 
label.config(relief = SUNKEN) 
look_button = ttk.Button(label, text = "Look") 
look_button.grid(row = 1, column = 1) 
look_button.config() 

#Create Game Frames 
frame_game = ttk.Frame(frame) 
frame_game.grid(row = 1, column = 2, sticky="nesw") 
frame_game.config(height = 480, width = 640) 
frame_game.config(relief = SUNKEN) 

我想有3張:

  • 一個在右上角的位置信息,
  • 一個正在發生的行爲底部右側,
  • 一個用於控制按鈕左上方填充。

我接下來嘗試的一切似乎毀了我迄今爲止的格式。

回答

0

使用rowspan如果您想讓左框填充多行。下面是如何做到這一點,我把每個框架中的Canvas帶有背景顏色,以便您可以看到它們的位置。這裏最主要的是我用左邊框架的rowspan = 2跨越2行。您應該移除畫布並將您的小部件放置在適當的框架中。

from tkinter import * 
from tkinter import ttk 

#create root window 
root = Tk() 
root.geometry('640x480+25+75') 
root.title('Dragon Slayer') 

#create Main frame 
frame = ttk.Frame(root) 
frame.grid(sticky= N+W+E+S) 
frame.config(height = 480, width = 640) 
frame.config(relief = SUNKEN) 

#create Left Controls frame 
frame_controls = ttk.Frame(frame) 
frame_controls.grid(row = 0, column = 0, rowspan = 2, sticky="nesw") 
frame_controls.config(height = 480, width = 125) 
frame_controls.config(relief = SUNKEN) 
can_l = Canvas(frame_controls, height = 480, width = 215, bg="green") 
can_l.grid() 

#Location Information Frames (top right) 
frame_loc = ttk.Frame(frame) 
frame_loc.grid(row = 0, column = 1, sticky="nesw") 
frame_loc.config(height = 180, width = 515) 
frame_loc.config(relief = SUNKEN) 
can_tr = Canvas(frame_loc, height = 180, width = 515, bg="red") 
can_tr.grid() 

#Create Game Frames (bottom right) 
frame_game = ttk.Frame(frame) 
frame_game.grid(row = 1, column = 1, sticky="nesw") 
frame_game.config(height = 480, width = 640) 
frame_game.config(relief = SUNKEN) 
can_br = Canvas(frame_game, height = 300, width = 515, bg="blue") 
can_br.grid() 

如果您想在窗口大小的框架擴​​大,再考慮使用grid_rowconfiguregrid_columnconfigure

這些職位可能會幫助:

Using row and column weights

Second related link

+0

我用帆布,幀的設置幾何形狀,設置窗口的幾何形狀製成的窗戶沒有可觀的,現在我有一個像放置一個文本控件我結束了一個灰色背景的新問題和我有畫布與背景設定圖像,所以我需要知道如何獲得透明背景文本小部件。因爲顯然使用化合物,您必須移動背景圖片或將文本居中...... –

0

因爲我問這個問題,我沒有足夠的研究來回答它自己,至少一個相當不錯的答案暫且。我將發佈我用於最終格式的代碼。

from tkinter import * 
from tkinter import ttk 

#create root window 
root = Tk() 
root.geometry('640x480+25+75') 
root.title('Dragon Slayer') 
root.resizable(0,0) 

#create Main frame 
frame = ttk.Frame(root) 
frame.grid(sticky= N+W+E+S) 
frame.config(height = 480, width = 640) 
frame.config(relief = SUNKEN) 

#create Left Controls frame 
frame_controls = Canvas(frame) 
frame_controls.pack(side = 'left') 
frame_controls.config(height = 480, width = 125) 
frame_controls.config(relief = SUNKEN) 
frame_controls.propagate(False) 
frame_controls_image = PhotoImage(file = "C:\\Users\\Garry\\Desktop\\Project\\parchment_left_small.gif") 
label = ttk.Label(frame_controls) 
label.config(image = frame_controls_image) 
label.pack() 
label.config(relief = SUNKEN) 

#Create Control buttons 
create_button = ttk.Button(label, text = "New Player") 
create_button.place(x =23, y =15) 
monastary_button = ttk.Button(label, state = 'disabled', text = "Monastary") 
monastary_button.place(x =23, y =55) 
fields_button = ttk.Button(label, state = 'disabled', text = "Fields") 
fields_button.place(x =23, y =95) 
item_shop_button = ttk.Button(label, state = 'disabled', text = "Item shop") 
item_shop_button.place(x =23, y =135) 
kill_button = ttk.Button(label, state = 'disabled', text = "Kill Player") 
kill_button.place(x =23, y =175) 

#Create Game Frames 
frame_location_image = PhotoImage(file = "C:\\Users\\Garry\\Desktop\\Project\\parchment_top.gif") 
frame_location = Canvas(frame) 
frame_location.pack(side = 'top') 
frame_location.config(height = 240, width = 515) 
frame_location.config(relief = SUNKEN) 
frame_location_label = ttk.Label(frame_location) 
frame_location_label.config(image = frame_location_image) 
frame_location_label.place(x=0,y=0) 
frame_location_label.config(compound = 'center') 

#create action frames 
frame_action_image = PhotoImage(file = "C:\\Users\\Garry\\Desktop\\Project\\parchment_bottom.gif") 
frame_action = Canvas(frame) 
frame_action.pack(side = 'bottom') 
frame_action.config(height = 240, width = 515) 
frame_action.config(relief = SUNKEN) 
frame_action_label = ttk.Label(frame_action, text = "Action") 
frame_action_label.config(image = frame_action_image) 
frame_action_label.place(x=0,y=0) 
frame_action_label.config(compound = 'center') 

#create stats window 
stats_window = Toplevel(root) 
stats_window.state('withdrawn') 
stats_window.title('Stats') 
stats_window.geometry('200x480+680+75') 
stats_window.resizable(True, True) 
frame_stats_background = PhotoImage(file = "C:\\Users\\Garry\\Desktop\\Project\\parchment_stats.gif") 
frame_stats_window = Canvas(stats_window) 
frame_stats_window.place(x=0, y=0) 
frame_stats_window.config(height = 480, width = 200) 
frame_stats_window.config(relief = SUNKEN) 
stats_file = open('C:\\Users\\Garry\\Desktop\\Project\\stats.txt', 'r') 
stats = stats_file.read() 
frame_stats_label = ttk.Label(frame_stats_window,text = stats) 
frame_stats_label.config(image = frame_stats_background, compound ='center') 
frame_stats_label.pack(side = 'left') 
frame_stats_label.config(font = "Helvetica 16 bold") 

#create monster stats window 
monster_stats_window = Toplevel(root) 
monster_stats_window.state('withdrawn') 
monster_stats_window.title('Monster Stats') 
monster_stats_window.geometry('200x480+900+75') 
monster_stats_window.resizable(True, True) 
monster_frame_stats_window = Canvas(monster_stats_window) 
monster_frame_stats_window.place(x=0, y=0) 
monster_frame_stats_window.config(height = 480, width = 200) 
monster_frame_stats_window.config(relief = SUNKEN) 
monster_stats_file = open('C:\\Users\\Garry\\Desktop\\Project\\monsterstats.txt', 'r') 
monster_stats = monster_stats_file.read() 
monster_frame_stats_label = ttk.Label(monster_frame_stats_window,text = monster_stats) 
monster_frame_stats_label.config(image = frame_stats_background, compound ='center') 
monster_frame_stats_label.pack(side = 'left') 
monster_frame_stats_label.config(font = "Helvetica 16 bold") 

#buttons 
kill_monster_button = ttk.Button(label, text = "Kill Monster") 
fight_button = ttk.Button(frame_action_label, text = "Fight") 
run_button = ttk.Button(frame_action_label, text = "Run!") 
look_button = ttk.Button(frame_action_label, text = "Look") 
leave_fields_button = ttk.Button(frame_action_label, text = "Leave") 
leave_item_shop_button = ttk.Button(frame_action_label, text = "Leave") 
heal_button = ttk.Button(frame_action_label, text = "Heal") 
revive_button = ttk.Button(frame_action_label, text = "Revive") 

I hope this helps someone else to find the answers they need. good luck to all