Python TksimpleDialog 定位在根窗口旁边 [英] Python TksimpleDialog positioning next to the root window
问题描述
我正在尝试打开一个简单的对话窗口,用户可以在其中根据根窗口上显示的菜单输入一个选项.然而,当我运行代码时,对话框会直接在根窗口中的菜单上方打开,使其看不见.有没有办法打开对话框,使其在根窗口旁边打开,如附图所示.
I am trying to open a simple dialog window, in which the user enters a choice based on a menu presented on the root window. When I run the code however the dialog opens directly above the menu in the root window obscuring it from sight. Is there a way to open the dialog so it opens next to the root window as shown in the attached image.
我已经检查了这个link 似乎没有任何定位简单对话框的参数.我也尝试过使用 toplevel,但是打开多个窗口时它变得混乱.
I have checked this link and it does not seem there is any positioning arguments for simple dialogs. I have also tried with toplevel but it got messy with multiple windows open.
我的代码如下:
from Tkinter import *
import tkSimpleDialog
root = Tk()
root.lift()
Label(root, text = "Menu Choices:").grid(row=1, column =0)
Label(root, text='1. Baloney and cheese').grid(row=2, column=0, pady=4)
Label(root, text='2. Roast chicken and gravy').grid(row=3, column=0, pady=4)
Label(root, text='3. Pear salad').grid(row=4, column=0, pady=4)
Label(root, text='4. Cateloupe and brocoli soup').grid(row=5, column=0, pady=4)
people = ["Liam","Henry","Paula"]
menuChoice = []
for i in people:
c = tkSimpleDialog.askinteger('Franks Restaurant', 'Please choose your meal?', parent = root)
menuChoice.append(c)
root.mainloop()
推荐答案
虽然 Tk 库名义上是私有的",但它实际上有一个执行此操作的函数.您可以按如下方式使用它.
the Tk library actually has a function that does this although it is nominally 'private'. You can use it as follows.
import tkinter as tk
root = tk.Tk()
root.wm_geometry("800x600")
dialog = tk.Toplevel(root)
root_name = root.winfo_pathname(root.winfo_id())
dialog_name = dialog.winfo_pathname(dialog.winfo_id())
root.tk.eval('tk::PlaceWindow {0} widget {1}'.format(dialog_name, root_name))
root.mainloop()
这会将您的对话框置于指定窗口(在本例中为根窗口)上方的中心.
This will place your dialog centred over the specified window (in this case th root window).
这篇关于Python TksimpleDialog 定位在根窗口旁边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!