【问题标题】:Tkinter formatting window layoutTkinter 格式化窗口布局
【发布时间】:2017-05-04 06:01:29
【问题描述】:

到目前为止,这是我的代码:

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 帧:

  • 右上角的位置信息,
  • 用于右下角发生的操作,
  • 一个用于控制按钮从上到下填充。

我接下来尝试的一切似乎都破坏了我目前的格式。

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    如果您希望左框架填充多于一行,请使用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

    【讨论】:

    • 我使用了画布,设置了框架的几何形状,设置了窗口的几何形状,使窗口变得不那么大,现在我遇到了新的问题,比如放置一个文本小部件,我最终得到了一个灰色的背景,我将画布设置为背景图像,所以我需要知道如何获取透明背景文本小部件。因为显然使用复合你必须移动背景图像或居中文本.......
    【解决方案2】:

    自从我问了这个问题以来,我做了足够的研究来自己回答这个问题,至少目前是一个相当不错的答案。我将发布我最终格式中使用的代码。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多