0

Here is my code so far:

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)

I want to have 3 frames:

  • one for location information on top right,
  • one for actions taking place bottom right,
  • one for control buttons left filling top to bottom.

Everything I try next seems to ruin the format I have so far.

j_4321
  • 15,431
  • 3
  • 34
  • 61
Garry Hurst
  • 93
  • 11

2 Answers2

0

Use rowspan if you want the left frame to fill more than one row. Here is how to do it, I put a Canvas in each frame with a background color so you can see their placement. The main thing here is I used rowspan = 2 for the left frame to span 2 rows. You should remove the canvas and place your widgets in the appropriate frames.

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()

If you want the frames to expand when the window is resized, then look into using grid_rowconfigure and grid_columnconfigure.

These posts might help:

Using row and column weights

Second related link

Community
  • 1
  • 1
Khristos
  • 973
  • 2
  • 11
  • 22
  • I used canvas, set geometry of frames, set geometry of windows made windows not sizable, now i have new problems like placing a text widget i end up with a grey background and i have the canvas set with a background image so i need to know how to get a transparent background text widget. since apparently using compound you either have to move the background image or center the text....... – Garry Hurst May 08 '17 at 00:18
0

Since I asked this question i did enough research to answer it myself, at least a fairly decent answer for the time being. I am going to post the code i used in my final format.

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
Garry Hurst
  • 93
  • 11