简体   繁体   English

使用 Window 使 TextCtrl 缩放

[英]Make TextCtrl Scale with Window

I've just gotten into wxpython for Python 3.6 and I've hit a roadblock.我刚刚进入 wxpython 的 Python 3.6 并且遇到了障碍。 I just can't work out how to make elements/widgets scale with the screen, I know you have to use sizers but that's about it.我只是不知道如何使元素/小部件随屏幕缩放,我知道你必须使用尺寸器,但仅此而已。 I'm still fairly new to programming so just reading the documentation didn't help.我对编程还是很陌生,所以仅仅阅读文档并没有帮助。 If someone could just sample some code that works I'd be very thankful as I could then read through it and work out what I was doing wrong, The code that draws out the GUI I want to scale with window size is below, the key idea is that the TextCtrl scales.如果有人可以对一些有效的代码进行采样,我将非常感激,因为我可以通读它并找出我做错了什么,绘制出我想用 window 大小缩放的 GUI 的代码如下,关键想法是 TextCtrl 缩放。 other elements don't really need scaling.其他元素并不真正需要缩放。

def createGUI(self):
        panel = wx.Panel(self)
        menuBar = wx.MenuBar()
        
        menuButton = wx.Menu()
        newItem = wx.MenuItem(menuButton, wx.ID_NEW, 'New Note\tCtrl+N')
        delItem = wx.MenuItem(menuButton, wx.ID_DELETE, 'Delete Note\tCtrl+Backspace')
        saveItem = wx.MenuItem(menuButton, wx.ID_SAVE, 'Save\tCtrl+S')
        exitItem = wx.MenuItem(menuButton, wx.ID_EXIT, 'Quit\tCtrl+Q')
        
        
        menuButton.Append(newItem)
        menuButton.Append(saveItem)
        menuButton.Append(delItem)
        menuButton.Append(exitItem)

        
        menuBar.Append(menuButton, 'Menu')
        self.SetMenuBar(menuBar)
        
        self.Bind(wx.EVT_MENU, self.new, newItem)
        self.Bind(wx.EVT_MENU, self.delete, delItem)
        self.Bind(wx.EVT_MENU, self.save, saveItem)
        self.Bind(wx.EVT_MENU, self.onExit, exitItem)
        
        self.noteText = wx.TextCtrl(panel)
        self.noteText.AppendText(self.notecontent)
        
        self.Bind(wx.EVT_CLOSE, self.onExit)
        self.SetTitle(f'Welcome {self.username}! You are working on {self.notepath}')
        self.Centre()
        self.Show(True)

To begin with it's best to equate sizers to something familiar and I usually think of storage boxes or a chest of drawers.首先,最好将 sizers 等同于熟悉的东西,我通常会想到储物盒或抽屉柜。
When we define widgets, they are all dumped into a container, the parent object, often the ubiquitous self or self.panel .当我们定义小部件时,它们都被转储到一个容器中,即parent object,通常是无处不在的selfself.panel
If we do not assign a size and pos to each item, it's just a jumbled mess, a pile of widgets.如果我们不给每个项目分配一个sizepos ,那它只是一团乱麻,一堆小部件。
The sizer , there are many types, are the virtual drawers in our chest of drawers, that herds this pile of widgets into order. sizer有很多类型,是我们抽屉柜中的虚拟抽屉,它将这堆小部件按顺序排列。
The widgets are assigned to the appropriate sizer , note sizers can go into other sizers, and eventually when everything has been assigned a place or drawer in our chest of drawers, the sizers do their magic, arranging and sizing all of widgets into a coherent screen for display or arranging the drawers contents and relative positions in the chest of drawers.小部件被分配给适当的尺寸器,注意尺寸器可以sizer到其他尺寸器中,最终当所有东西都被分配到我们的抽屉柜中的位置或drawer时,尺寸器会发挥它们的魔力,将所有小部件排列和调整大小到一个连贯的屏幕中用于展示或排列抽屉内容和在抽屉柜中的相对位置。
Below, I've used the simplest sizer a boxsizer.下面,我使用了最简单的sizer a boxsizer。
One will arrange things vertically and the other horizontally.一种是垂直排列,另一种是水平排列。
The horizontal sizer is for the buttons and the vertical sizer, will be the main sizer, into which I place not only the TectCtrl but also the buttons, prearranged in their horizontal sizer.水平尺寸器用于按钮,垂直尺寸器将是主要尺寸器,我不仅将TectCtrl还放置在其水平尺寸器中的按钮。
For a better and more comprehensive description of sizers and their controls see: https://docs.wxpython.org/sizers_overview.html and for details on the actual sizers available see the detailed documenation on each.有关 sizer 及其控件的更好和更全面的描述,请参阅: https://docs.wxpython.org/sizers_overview.html ,有关可用的实际 sizer 的详细信息,请参阅每个文件的详细文档。

A loose approximation of your code:您的代码的松散近似值:

#!/usr/bin/python

import wx
class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title,
            size=(450, 350))

        self.panel = wx.Panel(self, -1)

        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)

        self.noteText = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE)
        self.Button_close = wx.Button(self.panel, -1, label="Quit")
        self.Button_1 = wx.Button(self.panel, -1, label="Btn1")
        self.Button_2 = wx.Button(self.panel, -1, label="Btn2")

        self.Bind(wx.EVT_CLOSE, self.onExit)
        self.Button_close.Bind(wx.EVT_BUTTON, self.onExit)
        self.Button_1.Bind(wx.EVT_BUTTON, self.onButton)
        self.Button_2.Bind(wx.EVT_BUTTON, self.onButton)


        # Place buttons within their own horizontal sizer
        self.button_sizer.Add(self.Button_close,proportion=0, flag=wx.ALL, border=10)
        self.button_sizer.Add(self.Button_1,proportion=0, flag=wx.ALL, border=10)
        self.button_sizer.Add(self.Button_2,proportion=0, flag=wx.ALL, border=10)

        # Add textctrl and the button sizer to the main sizer (vertical)
        self.main_sizer.Add(self.noteText,proportion=1, flag=wx.EXPAND|wx.ALL, border=10)
        self.main_sizer.Add(self.button_sizer, 0, 0, 0)
        self.panel.SetSizer(self.main_sizer)
        self.Show()

    def onExit(self, event):
        self.Destroy()

    def onButton(self, event):
        print("A button was pressed")

if __name__ == '__main__':
    app = wx.App()
    Example(None, title="Example")
    app.MainLoop()

在此处输入图像描述

Edit: Here's the same code without using sizers, although they are recommended.编辑:尽管推荐使用sizer,但这是相同的代码,但不使用sizer。

#!/usr/bin/python

import wx
class Example(wx.Frame):

    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title,
            size=(450, 350))

        self.panel = wx.Panel(self, -1)

        self.noteText = wx.TextCtrl(self.panel, -1, pos=(10,10), size=(400,280), style=wx.TE_MULTILINE)
        self.Button_close = wx.Button(self.panel, -1, label="Quit", pos=(10,290), size=(50,30))
        self.Button_1 = wx.Button(self.panel, -1, label="Btn1", pos=(70,290), size=(50,30))
        self.Button_2 = wx.Button(self.panel, -1, label="Btn2", pos=(130,290), size=(50,30))

        self.Bind(wx.EVT_CLOSE, self.onExit)
        self.Button_close.Bind(wx.EVT_BUTTON, self.onExit)
        self.Button_1.Bind(wx.EVT_BUTTON, self.onButton)
        self.Button_2.Bind(wx.EVT_BUTTON, self.onButton)
        self.Show()

    def onExit(self, event):
        self.Destroy()

    def onButton(self, event):
        print("A button was pressed")

if __name__ == '__main__':
    app = wx.App()
    Example(None, title="Example")
    app.MainLoop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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