2017-05-04 38 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