2

我想制作一个 9x9 表,用户可以在其中插入值。正如您在代码中看到的那样,我使用了大量的语句来实现这个目的。我必须分别进行每个输入。我希望是否有一种方法可以让我使用 for 循环或其他方法来使这些语句消失。我不知道是否有可能做到这一点。请帮帮我。

另外,谁能告诉如何在应用程序的中心保留一个按钮?我不得不为此功能使用填充。还有其他方法吗?我试过style=Pack(alignment=CENTER)但没有成功。

import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW, CENTER, RIGHT

class Mine(toga.App):

    def startup(self):
        """
        Construct and show the Toga application.

        Usually, you would add your application to a main content box.
        We then create a main window (with a name matching the app), and
        show the main window.
        """

        self.t11 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t12 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t13 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t14 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t15 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t16 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t17 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t18 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t19 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        
        self.t21 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t22 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t23 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t24 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t25 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t26 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t27 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t28 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t29 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))

        self.t21 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t22 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t23 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t24 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t25 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t26 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t27 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t28 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t29 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))

        self.t31 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t32 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t33 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t34 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t35 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t36 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t37 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t38 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t39 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))

        self.t41 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t42 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t43 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t44 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t45 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t46 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t47 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t48 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t49 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))

        self.t51 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t52 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t53 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t54 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t55 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t56 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t57 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t58 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t59 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))

        self.t61 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t62 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t63 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t64 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t65 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t66 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t67 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t68 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t69 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))

        self.t71 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t72 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t73 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t74 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t75 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t76 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t77 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t78 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t79 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        
        self.t81 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t82 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t83 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t84 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t85 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t86 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t87 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t88 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t89 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))

        self.t91 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t92 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t93 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t94 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t95 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t96 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50, padding=(0,5,0,0)))
        self.t97 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t98 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
        self.t99 = toga.NumberInput(min_value=0, max_value=9, default=0, style=Pack(width=50))
##        selft = []
        self.row1 = toga.Box(
            children = [self.t11,
                        self.t12,
                        self.t13,
                        self.t14,
                        self.t15,
                        self.t16,
                        self.t17,
                        self.t18,
                        self.t19,],
            style = Pack(flex=1, alignment=CENTER)
            )
        self.row2 = toga.Box(
            children = [self.t21,
                        self.t22,
                        self.t23,
                        self.t24,
                        self.t25,
                        self.t26,
                        self.t27,
                        self.t28,
                        self.t29,],
            style = Pack(flex=1, alignment=CENTER)
            )
        self.row3 = toga.Box(
            children = [self.t31,
                        self.t32,
                        self.t33,
                        self.t34,
                        self.t35,
                        self.t36,
                        self.t37,
                        self.t38,
                        self.t39,],
            style = Pack(flex=1, alignment=CENTER, padding=(0,0,10,0))
            )
        self.row4 = toga.Box(
            children = [self.t41,
                        self.t42,
                        self.t43,
                        self.t44,
                        self.t45,
                        self.t46,
                        self.t47,
                        self.t48,
                        self.t49,],
            style = Pack(flex=1, alignment=CENTER)
            )
        self.row5 = toga.Box(
            children = [self.t51,
                        self.t52,
                        self.t53,
                        self.t54,
                        self.t55,
                        self.t56,
                        self.t57,
                        self.t58,
                        self.t59,],
            style = Pack(flex=1, alignment=CENTER)
            )
        self.row6 = toga.Box(
            children = [self.t61,
                        self.t62,
                        self.t63,
                        self.t64,
                        self.t65,
                        self.t66,
                        self.t67,
                        self.t68,
                        self.t69,],
            style = Pack(flex=1, alignment=CENTER, padding=(0,0,10,0))
            )
        self.row7 = toga.Box(
            children = [self.t71,
                        self.t72,
                        self.t73,
                        self.t74,
                        self.t75,
                        self.t76,
                        self.t77,
                        self.t78,
                        self.t79,],
            style = Pack(flex=1, alignment=CENTER)
            )
        self.row8 = toga.Box(
            children = [self.t81,
                        self.t82,
                        self.t83,
                        self.t84,
                        self.t85,
                        self.t86,
                        self.t87,
                        self.t88,
                        self.t89,],
            style = Pack(flex=1, alignment=CENTER)
            )
        self.row9 = toga.Box(
            children = [self.t91,
                        self.t92,
                        self.t93,
                        self.t94,
                        self.t95,
                        self.t96,
                        self.t97,
                        self.t98,
                        self.t99,],
            style = Pack(flex=1, alignment=CENTER)
            )

        self.button = toga.Button('Display', style=Pack(width=100, text_align=CENTER), on_press=self.print)
        self.b = toga.Box(style = Pack(flex=1, direction=ROW, padding=(10,180,10,180)))
        self.b.add(self.button)
        
        main_box = toga.Box(
            children=[self.row1,
                      self.row2,
                      self.row3,
                      self.row4,
                      self.row5,
                      self.row6,
                      self.row7,
                      self.row8,
                      self.row9,
                      self.b,],
            style=Pack(direction=COLUMN)
            )
        
        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def print(self, widget):
        self.t11.value=6
        self.t11.refresh()
##        self.num_label.text = self.num.value
##        self.num_label.refresh()

def main():
    return Mine()

我使用它得到的输出是: 在此处输入图像描述

4

1 回答 1

2

Yep, a simple for loop and list or dictionary could help you out. Maybe something like this to replace the input prompts?

self.t = []

for i in range(11, 100):
    if i % 10:
        if not str(i).endswith("3") and not str(i).endswith("6"):
            self.t.append(toga.NumberInput(min_value=0, max_value=9,
                                           default=0, style=Pack(width=50)))
        else:
             self.t.append(toga.NumberInput(min_value=0, max_value=9,
                                           default=0, style=Pack(width=50, padding=(0, 5, 0, 0))))

Then you could make the rows using:

self.row1 = toga.Box(
            children=[self.t[0]],
                        self.t[1],
                        self.t[2],
                        self.t[3],
                        self.t[4],
                        self.t[5],
                        self.t[6],
                        self.t[7],
                        self.t[8], ],
            style = Pack(flex=1, alignment=CENTER)
            )

I'm sure this can be improved but does this work with your code?

于 2020-10-27T22:26:18.170 回答