gpt4 book ai didi

python - Tkinter.pack() 方法不显示 GUI

转载 作者:行者123 更新时间:2023-11-28 18:56:24 29 4
gpt4 key购买 nike

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

        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)

GUI 甚至没有打开,而当我删除 pack() 行并使用网格时它可以工作但看起来极不平衡。

我的再现性完整代码是:

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 上使用 python 3.X,并且我遇到了按钮颜色的问题所以最终我从 tk.Button() ttk.Button()

希望得到一些帮助

最佳答案

确实,几何管理器不能在同一部分混合使用 packgrid。因此,您必须选择一种模式来组织窗口的布局,或者将窗口拆分为 2 个框架,以便能够将 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com