我试图熟悉pythonggui,因此为了做到这一点,我正在构建一个简单的计算器。
大部分都能工作,但当我使用以下代码时,我正试图在文本显示小部件中添加滚动条:

        self.scroll_bar = ttk.Scrollbar(self.master)
        self.scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
        # self.scroll_bar.grid(row=0, column=4)
        self.screen = tk.Text(self.master, height=5, width=30)
        # self.screen.grid(row=0, column=0, columnspan=4)
        self.screen.pack(side=tk.LEFT, fill=tk.BOTH)
        self.screen.configure(font=("Calibri", 20, "bold", "italic"),
                              foreground="black", background="whitesmoke")
        self.scroll_bar.config(command=self.screen.yview, orient=tk.VERTICAL)
        self.screen.config(yscrollcommand=self.scroll_bar.set)

当我移除pack()行并使用grid时,GUI甚至都没有打开,但看起来非常不平衡。
我对再现性的完整编码是:
import tkinter as tk
from tkinter import ttk
import re

# the calculator gui text for the buttons
calculator_button_text = {
    'clear': 'C',
    'zero': '0',
    'one': '1',
    'two': '2',
    'three': '3',
    'four': '4',
    'five': '5',
    'six': '6',
    'seven': '7',
    'eight': '8',
    'nine': '9',
    'decimal': '.',
    'plus': '+',
    'minus': '-',
    'multiply': 'x',
    'divide': u"\u00F7",
    'equal': '=',
    'delete': u"\u232B"
}


class CalculatorApp(tk.Frame):
    ''' Main Calculator Class '''

    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master
        self.user_in_list = []
        self.buttons_text = calculator_button_text
        self.equation = ""
        self.configure_gui()
        self.create_calc_widgets()
        # in order to change the button colors (mac)
        style = ttk.Style(self.master)
        style.theme_use('clam')
        style.configure('style.TButton', foreground="firebrick", background="darkgrey",
                        font=("Arial", 20, "bold", "italic"))
        style.map('TButton',
                  foreground=[('pressed', 'blue'),
                              ('active', 'darkgreen')],
                  background=[('pressed', 'dimgrey'),
                              ('active', 'grey')])

    def configure_gui(self):
        self.master.title("Calculator")
        self.master.configure(bg='black')

    def create_calc_widgets(self):
        self.create_input_field()
        self.create_buttons()

    def create_input_field(self):

        ## this work without scrollbar

        # self.screen = tk.Text(self.master, height=5, width=30)
        # self.screen.grid(row=0, column=0, columnspan=4, sticky=tk.W + tk.E)
        # self.screen.configure(font=("Calibri", 20, "bold", "italic"),
        #                       foreground="black", background="whitesmoke")

        self.scroll_bar = ttk.Scrollbar(self.master)
        self.scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
        # self.scroll_bar.grid(row=0, column=4)
        self.screen = tk.Text(self.master, height=5, width=30)
        # self.screen.grid(row=0, column=0, columnspan=4)
        self.screen.pack(side=tk.LEFT, fill=tk.BOTH)
        self.screen.configure(font=("Calibri", 20, "bold", "italic"),
                              foreground="black", background="whitesmoke")
        self.scroll_bar.config(command=self.screen.yview, orient=tk.VERTICAL)
        self.screen.config(yscrollcommand=self.scroll_bar.set)


    def create_buttons(self):
        button_col = 0
        button_row = 1
        text_in_row = ('C', '123d', '456-', '789x', '0.+/', '=')
        for row in text_in_row:
            for text in row:
                self.configure_button(text, button_row, button_col)
                button_col += 1
            button_col = 0
            button_row += 1

    def configure_button(self, txt, r, c):
        to_write = (txt == '=')
        # if txt in ('0123456789'):
        #     value = int(txt)

        if (txt == 'd'):
            txt = u"\u232B"
        elif (txt == '/'):
            txt = u"\u00F7"
        key = list(calculator_button_text.keys())[
            list(calculator_button_text.values()).index(txt)
        ]

        calculator_button_text[key] = ttk.Button(self.master, text=txt,
                                                 style='style.TButton',
                                                 command=lambda: self.click(txt, to_write))
        calculator_button_text[key].grid(row=r, column=c, ipady=10, ipadx=10)
        if (key=='clear'):
            self.configure_clear_button(key)
        elif (key=='equal'):
            self.configure_equal_button(key)

    def configure_clear_button(self, k):
        calculator_button_text[k].grid(columnspan=4, sticky=tk.W + tk.E)

    def configure_equal_button(self, k):
        calculator_button_text[k].grid(columnspan=4, sticky=tk.W + tk.E)

    def click(self, cmd, new_line):
        if (cmd != '='):
            self.display_on_screen(cmd, new_line)
        else:
            self.equation = re.sub(u"\u00F7", '/', self.equation)
            self.equation = re.sub("x", '*', self.equation)
            ans = str(eval(self.equation))
            self.display_on_screen(ans, new_line)
            self.equation = ""

    def display_on_screen(self, val, new_line=False):
        if (not new_line):
            self.screen.insert(tk.END, val)
            self.equation += str(val)
        else:
            self.screen.insert(tk.END, '\n')
            self.screen.insert(tk.END, val)
            self.screen.insert(tk.END, '\n')
            self.screen.yview('end')

    def clear_screen(self):
        # set equation to empty before deleting screen
        self.equation = ""
        self.screen.delete('1.0', tk.END)



if __name__ == '__main__':
    root = tk.Tk()
    calc_gui = CalculatorApp(root)
    # Make window fixed (cannot be resized)
    root.resizable(width=False, height=False)
    root.mainloop()

我会提到我在MacOS Mojave上使用Python3.X,我对按钮的颜色有问题,所以最终我从tk.Button()移到ttk.Button()
希望能帮上忙

最佳答案

实际上,几何图形管理器不能在同一节中混合packgrid。因此,您必须选择一种模式来组织窗口的布局,或者将窗口分成两个框架,以便能够在其中一个框架中pack小部件,在另一个框架中grid小部件。
您可以尝试以下操作:

self.screen = tk.Text(self.master, height=5, width=30)
self.screen.grid(row=0, column=0, columnspan=4, sticky='nswe')
self.scroll_bar = ttk.Scrollbar(self.master, command=self.screen.yview)
self.scroll_bar.grid(row=0, column=3, sticky='nse')

关于python - Tkinter.pack()方法不显示GUI,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58038099/

10-14 02:40