在 Python 中打包 GUI 小部件? (MAC)
Packing GUI widgets in Python? (MAC)
我对 Python 和学习 GUI 程序还很陌生。
两件事:
我希望问题以列表的形式出现(第二个问题及其答案框直接在第一个下面),但我不确定所有的定位功能。
此外,我想将 window 顶部的名称更改为 MPG 计算器(目前显示为 "tk")。
这是处理这部分程序的代码(如下)和输出照片
import tkinter
from tkinter import messagebox
class milesPerGallon:
def __init__(self):
# Make main window
self.main_window = tkinter.Tk()
# make frames
self.top_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)
# Make top widgets using .top_frame function
self.prompt_label = tkinter.Label(self.top_frame, \
text='How many gallons does the car hold? ')
self.gallons_entry = tkinter.Entry(self.top_frame, \
width=10)
self.prompt_label2 = tkinter.Label(self.top_frame, \
text='\nIn miles, how far can it go on a full tank? ')
self.miles_entry = tkinter.Entry(self.top_frame, \
width=10)
# Pack top widgets
self.prompt_label.pack(side='left')
self.gallons_entry.pack(side='left')
self.prompt_label2.pack(side='left')
self.miles_entry.pack(side='left')
# Make bottom wigdets using .bottom_frame function
self.calc_button = tkinter.Button(self.bottom_frame, \
text='Calculate', \
command=self.convert)
self.quit_button = tkinter.Button(self.bottom_frame, \
text='Quit', \
command=self.main_window.destroy)
# Pack buttons
self.calc_button.pack(side='left')
self.quit_button.pack(side='left')
# Pack frames
self.top_frame.pack()
self.bottom_frame.pack()
# Call Tkinter main loop.
tkinter.mainloop()
感谢您查看我的问题;感谢您的帮助!
试试这个:
self.main_window.title("MPG Calculator")
通过添加 .title
命令,您可以更改页面的名称。
我无法测试您的 GUI,所以也许它是一个需要更改的不同页面,但它通过相同的原理工作。
因为我无法测试您的 GUI,所以我还没有解决您的其他问题的方法。
我对 Python 和学习 GUI 程序还很陌生。 两件事:
我希望问题以列表的形式出现(第二个问题及其答案框直接在第一个下面),但我不确定所有的定位功能。
此外,我想将 window 顶部的名称更改为 MPG 计算器(目前显示为 "tk")。
这是处理这部分程序的代码(如下)和输出照片
import tkinter
from tkinter import messagebox
class milesPerGallon:
def __init__(self):
# Make main window
self.main_window = tkinter.Tk()
# make frames
self.top_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)
# Make top widgets using .top_frame function
self.prompt_label = tkinter.Label(self.top_frame, \
text='How many gallons does the car hold? ')
self.gallons_entry = tkinter.Entry(self.top_frame, \
width=10)
self.prompt_label2 = tkinter.Label(self.top_frame, \
text='\nIn miles, how far can it go on a full tank? ')
self.miles_entry = tkinter.Entry(self.top_frame, \
width=10)
# Pack top widgets
self.prompt_label.pack(side='left')
self.gallons_entry.pack(side='left')
self.prompt_label2.pack(side='left')
self.miles_entry.pack(side='left')
# Make bottom wigdets using .bottom_frame function
self.calc_button = tkinter.Button(self.bottom_frame, \
text='Calculate', \
command=self.convert)
self.quit_button = tkinter.Button(self.bottom_frame, \
text='Quit', \
command=self.main_window.destroy)
# Pack buttons
self.calc_button.pack(side='left')
self.quit_button.pack(side='left')
# Pack frames
self.top_frame.pack()
self.bottom_frame.pack()
# Call Tkinter main loop.
tkinter.mainloop()
感谢您查看我的问题;感谢您的帮助!
试试这个:
self.main_window.title("MPG Calculator")
通过添加 .title
命令,您可以更改页面的名称。
我无法测试您的 GUI,所以也许它是一个需要更改的不同页面,但它通过相同的原理工作。
因为我无法测试您的 GUI,所以我还没有解决您的其他问题的方法。