我无法在需要的位置打开自定义对话框弹出窗口。我已经尝试过self.geometry("+%d+%d" % (x1, x2))
,但没有成功。理想情况下,我希望在鼠标指针的位置打开它。
通过菜单命令调用对话框:
menu = tk.Menu(self.treeDocs, tearoff=0)
menu.add_command(label="Check-In",
command=lambda tv=self.treeDocs: self.CheckIn(tv))
def CheckIn(self, tv):
cd = CalendarDialog(self)
docDate = cd.result
if docDate is not None:
docID = tv.set(self.ident, "ID")
conn = pyodbc.connect(strConn)
cursor = conn.cursor()
strSQL = '''INSERT INTO tblDocDates (ID_DOC, ACCESSDATE)
VALUES (%s, #%s#)''' % (docID, docDate)
cursor.execute(strSQL)
cursor.commit()
这是课程:
class CalendarDialog(tk.simpledialog.Dialog):
def __init__(self, master, title="Check-In"):
tk.simpledialog.Dialog.__init__(self, master, title="Check-In")
def body(self, master):
self.calendar = tkcalendar.DateEntry(master)
self.calendar.pack()
def apply(self):
self.result = self.calendar.get()
谢谢您的帮助!
最佳答案
问题:设置Tkinter Dialog
的位置?
您正在使用在simpledialog.Dialog
中阻塞直到被销毁的__init__
。
要使用Dialog
放置self.geometry(...
,您必须挂接到self.deiconify(...
。
import tkinter as tk
from tkinter import simpledialog
class CalendarDialog(simpledialog.Dialog):
def __init__(self, master, coord=(0, 0), title=None):
self.coord = coord
super().__init__(master, title=title)
# super() is blocking
def deiconify(self):
x, y = self.coord
self.geometry('{width}x{height}+{x}+{y}'\
.format(width=200, height=100, x=x, y=y))
super().deiconify()
用法:
class App(tk.Tk):
def __init__(self):
super().__init__()
self.menubar = tk.Menu(self, tearoff=0)
self.config(menu=self.menubar)
self.menubar.add_command(label="Check-In",
command=lambda x=50, y=self.menubar.yposition(1):
self.CheckIn(x, y))
def CheckIn(self, x, y):
coord = self.winfo_rootx() + x, self.winfo_rooty() + y
cd = CalendarDialog(self, title='Check_In', coord=coord)
if __name__ == "__main__":
App().mainloop()
使用Python测试:3.5-'TclVersion':8.6'TkVersion':8.6
关于python - 如何设置“Tkinter对话框”弹出窗口的位置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58089026/