简体   繁体   中英

Python tkinter downsizing widgets

I've looked at all the other questions and answers and couldn't find one that fit what I'm trying to do.

Code:

class GameWin:
    def __init__(self, master):
        self.master = master
        self.master.title("Title")

        self.main_frame = Frame(self.master, bd = 10, bg = uni_bg)
        self.main_frame.grid()

        self.left_frame = Frame(self.main_frame, bd = 10, bg = uni_bg)
        self.left_frame.grid(column = 0, row = 0)

        self.right_frame = Frame(self.main_frame, bd = 10, bg = uni_bg)
        self.right_frame.grid(column = 1, row = 0)

        self.right_frame.columnconfigure(0, weight = 1)
        self.right_frame.rowconfigure(0, weight = 1)
        self.right_frame.columnconfigure(1, weight = 1)
        self.right_frame.rowconfigure(1, weight = 1)

        self.web = Text(self.left_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bt_bg, fg = uni_fg)
        self.web.grid(column = 0, row = 0, padx = 5, pady = 5)

        self.output = Text(self.left_frame, font = (uni_font, 12), wrap = WORD, bg = "black", fg = uni_fg)
        self.output.grid(column = 0, row = 1, padx = 5, pady = 5, sticky = "ew")
        self.output.configure(state = "disabled")

        self.input = Entry(self.left_frame, font = (uni_font, 12))
        self.input.grid(column = 0, row = 2, sticky = "ew")
        self.input.bind('<Return>', self.submit)

        self.notepad = Text(self.right_frame, font = (uni_font, 12), wrap = WORD, bg = uni_fg, fg = "black", width = 42)
        self.notepad.grid(column = 0, row = 0, pady = 5, rowspan = 2)

        self.sys_info = Text(self.right_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bg, fg = uni_fg, width = 35, height = 11, bd = 0)
        self.sys_info.tag_configure('center', justify='center')
        self.sys_info.grid(column = 1, row = 0, pady = 5)
        self.sys_info.insert(END, "NAME", "center")
        self.sys_info.configure(state = "disabled")

        self.trace = Text(self.right_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bg, fg = uni_fg, width = 35, height = 11)
        self.trace.grid(column = 1, row = 1, pady = 5)

        self.email = Text(self.right_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bt_bg, fg = uni_fg)
        self.email.grid(column = 0, row = 2, pady = 5, columnspan = 2)
        self.email.configure(state = "disabled")

        self.respond = Entry(self.right_frame, font = (uni_font, 12))
        self.respond.grid(column = 0, row = 3, columnspan = 2, sticky = "ew")
        self.respond.bind('<Return>', self.do_respond)

    def submit(self, event):
        self.output.configure(state = "normal")
        self.output.configure(state = "disabled")
        pass

    def do_respond(self, event):
        pass

Image of current screen: https://i.imgur.com/P2B6E5y.png

First thing I'm trying to figure out is how to not explicitly state the size of the 3 text widgets in the top right. Because everyone's screen is differently sized. If I don't explicitly state the size, they expand and everything goes wacko (since the default text widget is big). I want the widgets to automatically downscale to fit within the column (the same width as the big grey bottom right text widget). Is this even possible?

Second is for the frames and widgets to fill up all the space in the window. Whether it's fullscreen (like in the pic) or a smaller window (and hopefully keep their size relative to each other). There's a lot of empty space at the edges of the window and I want to get rid of that. I've tried everything I can think of but I can't get them to fill that space.

I tried putting the top 3 widgets each in their own frame, limiting the size of the frames relative to the window size, and setting the widgets to fill that frame but it doesn't work. Code I used to try this: https://pastebin.com/3YWK9Xg2

class GameWin:
    def __init__(self, master):
        self.master = master
        self.master.title("Hacker")
        win_width = self.master.winfo_width()
        win_height = self.master.winfo_height()

        self.main_frame = Frame(self.master, bd = 10, bg = uni_bg)
        self.main_frame.grid(sticky = "nsew")

        self.left_frame = Frame(self.main_frame, bd = 10, bg = uni_bg, height = int(win_height), width = int(win_width/2))
        self.left_frame.grid(column = 0, row = 0, rowspan = 3)
        self.left_frame.grid_propagate(False)

        self.note_frame = Frame(self.main_frame, bd = 10, bg = uni_bg, height = int(win_height/2), width = int(win_width/4))
        self.note_frame.grid(column = 1, row = 0, rowspan = 2, sticky = "n")
        self.note_frame.grid_propagate(False)

        self.sys_frame = Frame(self.main_frame, bd = 10, bg = uni_bg, height = int(win_height/4), width = int(win_width/4))
        self.sys_frame.grid(column = 2, row = 0, sticky = "n")
        self.sys_frame.grid_propagate(False)

        self.trace_frame = Frame(self.main_frame, bd = 10, bg = uni_bg, height = int(win_height/4), width = int(win_width/4))
        self.trace_frame.grid(column = 2, row = 1, sticky = "n")
        self.trace_frame.grid_propagate(False)

        self.bottom_right_frame = Frame(self.main_frame, bd = 10, bg = uni_bg, height = int(win_height/2), width = int(win_width/2))
        self.bottom_right_frame.grid(column = 1, row = 2, columnspan = 2)
        self.bottom_right_frame.grid_propagate(False)

        self.web = Text(self.left_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bt_bg, fg = uni_fg)
        self.web.grid(column = 0, row = 0, padx = 5, pady = 5)

        self.output = Text(self.left_frame, font = (uni_font, 12), wrap = WORD, bg = "black", fg = uni_fg)
        self.output.grid(column = 0, row = 1, padx = 5, pady = 5, sticky = "ew")


        self.input = Entry(self.left_frame, font = (uni_font, 12))
        self.input.grid(column = 0, row = 2, sticky = "ew")
        self.input.bind('<Return>', self.submit)

        self.notepad = Text(self.note_frame, font = (uni_font, 12), wrap = WORD, bg = uni_fg, fg = "black")
        self.notepad.pack(fill = BOTH, expand = YES)

        self.sys_info = Text(self.sys_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bg, fg = uni_fg)
        self.sys_info.tag_configure('center', justify='center')
        self.sys_info.grid(sticky = "nsew")
        self.sys_info.insert(END, "NAME", "center")
        self.sys_info.configure(state = "disabled")

        self.trace = Text(self.trace_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bg, fg = uni_fg)
        self.trace.grid(sticky = "nsew")

        self.email = Text(self.bottom_right_frame, font = (uni_font, 12), wrap = WORD, bg = uni_bt_bg, fg = uni_fg)
        self.email.grid(row = 0, pady = 5, columnspan = 2, sticky = "nsew")
        self.email.configure(state = "disabled")

        self.respond = Entry(self.bottom_right_frame, font = (uni_font, 12))
        self.respond.grid(row = 1, columnspan = 2, sticky = "ew")
        self.respond.bind('<Return>', self.do_respond)

    def submit(self, event):
        self.output.configure(state = "normal")
        self.output.configure(state = "disabled")

    def do_respond(self, event):
        pass

and picture of the result: https://i.imgur.com/IVnw65x.png

Here is the full code: https://pastebin.com/Gm2ePqFH . I want it to look like it is in the first picture, without having to explicitly state the size of each text widget. And I want to get it to all stay the same size relative to the window.

If you want widgets to shrink down to the size of the column, the strategy that has worked best for me is to make the widget very small and then use the layout manager ( pack , place , or grid ) make them bigger to fit. You can either make the widget 1x1 if that's truly a minimum size you will accept, or you can set it to what you think the absolute minimum should be (for example, 4 lines of 20 characters, 20 lines of 10 characters, etc).

So, start by making those widgets as small as possible.

Next, make sure you use the sticky attribute so that the widgets grow to fill their allotted space. You also need to make sure you use the sticky attribute for self.right_frame so that it fills its space too.

Finally, make sure that you call rowconfigure and columnconfigure to set a positive weight on any widget that has children managed by grid . You aren't doing that for self.master , nor are you doing it for self.left_frame and self.right_frame

As a rule of thumb, if you're only putting one or two widgets in a frame and you want those widgets to fill the frame, it's much better to use pack than grid , simply because you don't have to remember to give the rows and columns weights.

For example, you can use pack to manage the left and right frames. You can probably use pack to put GameWin inside of its master, too.

Also, don't try to solve all of your layout problems at once. In your case, I would tackle the problem like this:

  • Start with just your mainframe and get it so that it fills the containing window. Make sure you manually resize the window to watch it grow and shrink.

  • Next, add your left and right frames. Make sure they grow and shrink to fit mainframe .

  • Next, focus on just the widgets in the left frame. Add them, and make sure they shrink and fit.

  • Then, focus on the right frame. Add them, and make sure they shrink and fit.

Finally, a word of advice. Group your calls to grid together. As your code is written now, you need to fix a whole bunch of lines scattered over the entire file. With some reorganization, almost all of the lines of code you need to change will be in one block of code.

For example, instead of this:

self.web = Text(...)
self.web.grid(...)
self.output = Text(...)
self.output.grid(...)
self.input = Text(...)
self.input.grid(...)
self.notepad = Text(...)
self.notepad.grid(...)

Do this:

self.web = Text(...)
self.output = Text(...)
self.input = Text(...)
self.notepad = Text(...)

self.web.grid(...)
self.output.grid(...)
self.input.grid(...)
self.notepad.grid(...)

With that, at a glance you can answer the question "how are my widgets defined?", and "how are my widgets laid out". With the way you have it now, those questions are very difficult to answer.

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