简体   繁体   中英

Why does my python program cut off a good section of my tkinter output?

I'm programming a simple application to help with tracking combat information for dnd using python and tkinter. I started with the input page and it worked fine, but when I started to use tabs to allow the user to access a help page, strangely it cut off about half of the original output, the only thing I changed is using ttk and my_frame 1 instead of root. I've tried changing .place to .grid and it worked to some degree, although instead it displayed the buttons in strange positions in front of the rest of the outputs. Then I tried adding blank lines on the help tag, this did work but it seems quite ineffective and time consuming. I can guess from this there's no space but the geometry was set to 500x500 which should be plenty. So I'm trying to figure out what's going wrong. Thanks for reading! (Also it isn't quite complete yet hence why the next page function does something random) This is the code:

import tkinter as tk
from tkinter import ttk
#Starting up tkinter
root = tk.Tk()
root.title("Dungeons and dragons combat manager")
root.geometry("500x500")
root.configure(background = "White")
#Initialise the arrays
player_details = []
overall_data = []
#help page
my_notebook = ttk.Notebook(root)
my_notebook.pack(pady = 15)
my_frame1 = tk.Frame(my_notebook, width = "500", height = "500")
my_frame1.grid(row = 0, column = 0, columnspan = 2, padx = 40, pady = 5)
my_frame2 = tk.Frame(my_notebook, width = 500, height = 500)
my_notebook.add(my_frame1, text = "Main")
my_notebook.add(my_frame2, text = "Help")
line1 = tk.Label(my_frame2, text = "Hello, welcome to the help page!", font = ('Times New Roman', 12)).grid(row = 0, column = 0, sticky = 'W')
line2 = tk.Label(my_frame2, text = "Most of the instructions will be given during application use", font = ('Times New Roman', 12)).grid(row = 1, column = 0, sticky = 'W')
line3 = tk.Label(my_frame2, text = "Please ensure all numerical inputs are given in integers", font = ('Times New Roman', 12)).grid(row = 2, column = 0, sticky = 'W')
line4 = tk.Label(my_frame2, text = "If any mistakes are made please press the restart button", font = ('Times New Roman', 12)).grid(row = 3, column = 0, sticky = 'W')
line5 = tk.Label(my_frame2, text = "Once the order is displayed initiative cannot be changed", font = ('Times New Roman', 12)).grid(row = 4, column = 0, sticky = 'W')
line6 = tk.Label(my_frame2, text = "However, health can be edited at any time", font = ('Times New Roman', 12)).grid(row = 5, column = 0, sticky = 'W')
line7 = tk.Label(my_frame2, text = "Good luck with your encounter!", font = ('Times New Roman', 12)).grid(row = 6, column = 0, sticky = 'W')
#main section
tk.Label(my_frame1, text = "Player details form", font = ('Times New Roman', 16)).grid(row = 0, column = 0, padx = 25, pady = 5)
#function definitions
def submit():
   player_details = []
   player_details.append(name.get())
   player_details.append(health.get())
   player_details.append(ac.get())
   player_details.append(initiative.get())
   overall_data.append(player_details)
   print(str(overall_data))
   player_num = tk.Label(root, text = "Inputting for Player :" + str(len(overall_data)+1), fg = 'White', bg = "Black")
   player_num.place(x = 30, y = 210, width = 240, height = 25)

def clear():
   name.delete(0, 'end')
   health.delete(0, 'end')
   ac.delete(0, 'end')
   initiative.delete(0, 'end')
   name.focus_set()
def next_page():
   print("chicken")
#display
player_data = ['Name: ','Health: ','Armour Class: ','initiative: ']
labels = range(4)
for i in range(4):
   bg_colour = "Black"
   l = tk.Label(my_frame1, 
                text = player_data[i], 
                fg='White', 
                bg=bg_colour)
   l.place(x = 20, y = 60 + i*30, width=120, height=25)
name = tk.Entry(my_frame1,width = 15,fg = "Black",bg = "White")
name.place(x = 150, y = 60, width = 120, height=25)
health = tk.Entry(my_frame1, width = 15,fg = "Black",bg = "White")
health.place(x = 150, y = 90, width = 120, height=25)
ac = tk.Entry(my_frame1, width = 15,fg = "Black",bg = "White")
ac.place(x = 150, y = 120, width = 120, height=25)
initiative = tk.Entry(my_frame1, width = 15,fg = "Black",bg = "White")
initiative.place(x = 150, y = 150, width = 120, height=25)
submit_button = tk.Button(my_frame1, text = "Submit", command = submit)
submit_button.place(x = 30, y = 180, width = 100, height = 25)
clear_button = tk.Button(my_frame1, text = "Clear", command = clear)
clear_button.place(x = 160, y = 180, width = 100, height = 25)
player_num = tk.Label(my_frame1, text = "Inputting for Player : 1", fg = 'White', bg = "Black")
player_num.place(x = 30, y = 210, width = 240, height = 25)
next_button = tk.Button(my_frame1, text = "Next", command = next_page)
next_button.place(x = 30, y = 240, width = 100, height = 25)
'''

It is because you haven't made the notebook large enough, and haven't requested that it expand to fill the available space. You're relying on the default size of the notebook, and you're putting more things in than will fit. Plus, you're using place instead of pack inside the notebook, and place won't cause the containing window to grow or shrink. This is one of the disadvantages to using place as a general purpose layout mechanism.

The easy solution is to use the options that pack supports, such as fill and expand , though without knowing what your full UI should look like it's hard to say if that's the correct solution or not.

my_notebook.pack(fill="both", expand=True)

截屏

Another solution would be to stop using place . If you use grid and/or pack , then the notebook will resize to fit its contents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM