两种方法:1.在QSplitter创建实例的时候,构造函数传递主窗口的参数。
2.使用QLayout子类管理窗口
1例:QSplitter *mysp = new QSplitter(this)//this指针是主窗口
2例:QGridLayout *mainly = new QGridLayout//也可以使用水平对齐或垂直对齐
mainly->add(mysp,0,0)
this->setLayout(mysp)
python中tkinter需要使用accelerator添加菜单的快捷键,该选项仅显示,并没有实现加速键的功能,添加功能需按键绑定,代码如下:from tkinter import *
root = Tk()
def callback():
print("~被调用了~")
# 创建一个顶级菜单
menubar = Menu(root)
# 创建一个下拉菜单“文件”,然后将它添加到顶级菜单中
filemenu = Menu(menubar, tearoff=False)
filemenu.add_command(label="打开", command=callback, accelerator='Ctrl+N')
filemenu.add_command(label="保存", command=callback)
filemenu.add_separator()
filemenu.add_command(label="退出", command=root.quit)
menubar.add_cascade(label="文件", menu=filemenu)
# 显示菜单
root.config(menu=menubar)
root.bind_all("<Control-n>", lambda event: print('加速键Ctrl+N'))
mainloop()
#!/usr/bin/env python3 # py 3.6+"""
#要求做一个系统菜单,输入数字进入对应菜单,包含以下内容,正常 *** 作不能报错:
# 菜单2:修改产品价格
# 菜单3:增加库存
# 菜单4:购买指定数量产品
# 菜单5:增加新产品 作为思考题
# 菜单0:退出当前系统
"""
price = {'vegetables': '3','eggs': '4','rice': '2'} # 价格dict
stock = {'vegetables': '0','eggs': '0','rice': '0'} # 库存dict
tip = '''
1:列印所有产品价格和库存
2:修改产品价格
3:增加库存
4:购买指定数量产品
5:增加新产品 作为思考题
0:退出当前系统
'''
def main():
while True:
global price, stock
a = input(f'Please enter a number:{tip}\n').strip()
if a == '0':
print('Exit!')
break
elif a == '1':
style = '{:15}{:6}{:5}'
print(style.format('Name', 'price', 'stock'))
for (n, p), (_, s) in zip(price.items(), stock.items()):
print(style.format(n, p, s))
print()
elif a == '2':
while True:
n = input('enter a product name to modify its price: ')
if n in price:
break
print('invalid input! Should be "{}".'.format(
'" or "'.join(price)))
p = input('enter a new price of this product: ')
price[n] = p
elif a == '3':
while True:
n = input('enter a product name to increase its stock: ')
if n in stock:
break
print('Invalid input! Should be "{}".'.format(
'" or "'.join(stock)))
while True:
s = input('enter a integer to update the stock of it: ')
try:
s = int(s)
break
except:
print('Invalid input, must be a integer!')
stock[n] = str(int(stock[n]) + s)
elif a == '4':
while True:
n = input('enter a product name to buy it: ')
if n in stock:
break
print('Invalid input! Should be "{}".'.format(
'" or "'.join(stock)))
while True:
s = input('enter a integer for how many to buy: ')
try:
s = int(s)
if s <=0 or s > int(stock[n]):
raise
break
except:
print('Invalid input, must be a positive integer and '
'less than{}!'.format(stock[n]))
y = input('You want to buy {} X {}, which cost {}? (y)/n '.format(
n, s, int(price[n]) * s))
if y.strip().lower() in ('y', ''):
stock[n] = str(int(stock[n]) - s)
print('You pay {} and get {} {}'.format(int(price[n]*s), s, n))
elif a == '5':
print('Uncomplete...\n')
if __name__ == '__main__':
main()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)