我正在尝试打开一个简单的对话窗口,用户可以根据根窗口中显示的菜单输入选项。当我运行代码时,对话框会在根窗口的菜单正上方打开,从而遮挡它。有没有办法打开对话框,使其在根窗口旁边打开,如附图所示。
我检查了这个link,似乎没有任何简单对话框的定位参数。我也试过顶级但是打开了多个窗户弄得很乱。
我的代码如下:
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()
答案 0 :(得分:0)
一种方法是使用Toplevel小部件。而且它根本不是凌乱的。无论如何,要将输入框架放在所需的位置,首先要设置尺寸和主(根)框架的位置:
from Tkinter import *
import ttk
root = Tk()
root.lift()
w = 300; h = 200; x = 10; y = 10
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
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)
def store_entry():
print "Entry stored as "+ent.get()
def exit_entry():
print "Entry cancelled"
top.destroy()
top = Toplevel()
top.title('Franks Restaurant')
top.geometry("%dx%d+%d+%d" % (w, h, w+x+20, y))
Label(top, text='Please choose your meal').place(x=10,y=10)
ent = Entry(top); ent.place(x=10, y=50); ent.focus()
Button(top, text="OK", command=store_entry).place(x=10, y=150)
Button(top, text="Cancel", command=exit_entry).place(x=60, y=150)
root.mainloop()
这是将用户输入窗口放置在您想要的位置的示例。您需要实现它以验证和存储用户输入以及您需要的多个用户。
答案 1 :(得分:0)
Tk库实际上有一个功能可以做到这一点,虽然它名义上是私人的#39;你可以按照以下方式使用它。
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()
这会将对话框置于指定窗口的中心位置(在本例中为根窗口)。
答案 2 :(得分:0)
'
import tkinter as tk
try: # allows the text to be more crisp on a high dpi display
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except:
pass
# ----------------------------------------------------------------------------
class CustomDialog:
def __init__(self, parent):
self.top = tk.Toplevel(parent)
xPos = root.winfo_x()
yPos = root.winfo_y()
posStr = "+" + str(xPos + 160) + "+" + str(yPos)
self.top.geometry(posStr)
tk.Label(self.top, text="Dlg is positioned relative to root window.").pack()
tk.Button(self.top, text="Ok", command=self.Ok).pack()
def Ok(self):
self.top.destroy()
# ----------------------------------------------------------------------------
def onOpenDlg():
inputDialog = CustomDialog(root)
root.wait_window(inputDialog.top)
# ----------------------------------------------------------------------------
root = tk.Tk()
tk.Label(root, text="Position Dlg Example").pack()
tk.Button(root, text="Open Dlg", command=onOpenDlg).pack()
root.mainloop()
'