本文整理汇总了Python中tkinter.Tk.bind方法的典型用法代码示例。如果您正苦于以下问题:Python Tk.bind方法的具体用法?Python Tk.bind怎么用?Python Tk.bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tkinter.Tk
的用法示例。
在下文中一共展示了Tk.bind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def main():
'''runs the program'''
square_width = 64
num_cols = 7
num_rows = 6
canvas_width = (num_cols+1)*square_width
canvas_height = (num_rows+1)*square_width
window = Tk()
window.configure(background='black')
window.title("DynaBLASTER")
window.resizable(0,0) #removes maximize option
#window.iconbitmap('icon.ico')
#window.tk.call('tk', 'scaling', 20.0)
canvas = Canvas(window, width=canvas_width, highlightthickness=0,
height=canvas_height, background='#717171')
canvas.grid(row=1,column=0, columnspan=5)
graphics = Graphics(canvas, num_rows, num_cols, square_width, window)
board = Board(canvas, square_width, num_rows, num_cols,
canvas_width, canvas_height)
col=0
row=0
player1 = Player(canvas, board, square_width, graphics, col, row)
col = graphics.cols - 3
row = graphics.rows - 3
player2 = Player(canvas, board, square_width, graphics, col, row)
# Import settings from bindings file
bindings_file = open('bindings.json')
p1_bindings, p2_bindings, gen_bindings = json.load(bindings_file)
window.bind(key_release_of(p1_bindings["Up"]), lambda event:player1.key_release('Up'))
window.bind(key_release_of(p1_bindings["Down"]), lambda event:player1.key_release('Down'))
window.bind(key_release_of(p1_bindings["Left"]), lambda event:player1.key_release('Left'))
window.bind(key_release_of(p1_bindings["Right"]), lambda event:player1.key_release('Right'))
window.bind(p1_bindings["Up"], lambda event:player1.key_press('Up'))
window.bind(p1_bindings["Down"],lambda event:player1.key_press('Down'))
window.bind(p1_bindings["Left"], lambda event:player1.key_press('Left'))
window.bind(p1_bindings["Right"], lambda event:player1.key_press('Right'))
window.bind(p1_bindings["Bomb"], player1.place_bomb)
window.bind(key_release_of(p2_bindings["Up"]), lambda event:player2.key_release('Up'))
window.bind(key_release_of(p2_bindings["Down"]), lambda event:player2.key_release('Down'))
window.bind(key_release_of(p2_bindings["Left"]), lambda event:player2.key_release('Left'))
window.bind(key_release_of(p2_bindings["Right"]), lambda event:player2.key_release('Right'))
window.bind(p2_bindings["Up"], lambda event:player2.key_press('Up'))
window.bind(p2_bindings["Down"], lambda event:player2.key_press('Down'))
window.bind(p2_bindings["Left"], lambda event:player2.key_press('Left'))
window.bind(p2_bindings["Right"], lambda event:player2.key_press('Right'))
window.bind(p2_bindings["Bomb"], player2.place_bomb)
window.bind(gen_bindings["Pause"], lambda event:pause_game(player1, player2, graphics))
window.mainloop()
示例2: __init__
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def __init__(self, num_rows, num_cols, best_possible):
self.rows = num_rows
self.cols = num_cols
self.expectedBest = best_possible
# create the root and the canvas
root = Tk()
# local function to bind/unbind events
self.bind = root.bind
self.unbind = root.unbind
# local function to change title
self.updateTitle = root.title
# local function to change cursor
self.updateCursor = lambda x: root.config(cursor=x)
# local function to start game
self.start = root.mainloop
# get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# fix scaling for higher resolutions
if max(self.canvasWidth / ws, self.canvasHeight / hs) < 0.45:
self.scale = 2
self.canvas = Canvas(root, width=self.canvasWidth, height=self.canvasHeight)
self.canvas.pack()
# calculate position x, y
x = (ws - self.canvasWidth) // 2
y = (hs - self.canvasHeight) // 2
root.geometry('%dx%d+%d+%d' % (self.canvasWidth, self.canvasHeight, x, y))
root.resizable(width=0, height=0)
self.init()
# set up keypress events
root.bind("<Key>", self.keyPressed)
示例3: demo
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def demo():
root = Tk()
root.bind('<Control-q>', lambda e: root.destroy())
table = Table(root, 'Word Synset Hypernym Hyponym'.split(),
column_weights=[0, 1, 1, 1],
reprfunc=(lambda i,j,s: ' %s' % s))
table.pack(expand=True, fill='both')
from nltk.corpus import wordnet
from nltk.corpus import brown
for word, pos in sorted(set(brown.tagged_words()[:500])):
if pos[0] != 'N': continue
word = word.lower()
for synset in wordnet.synsets(word):
hyper = (synset.hypernyms()+[''])[0]
hypo = (synset.hyponyms()+[''])[0]
table.append([word,
getattr(synset, 'definition', '*none*'),
getattr(hyper, 'definition', '*none*'),
getattr(hypo, 'definition', '*none*')])
table.columnconfig('Word', background='#afa')
table.columnconfig('Synset', background='#efe')
table.columnconfig('Hypernym', background='#fee')
table.columnconfig('Hyponym', background='#ffe')
for row in range(len(table)):
for column in ('Hypernym', 'Hyponym'):
if table[row, column] == '*none*':
table.itemconfig(row, column, foreground='#666',
selectforeground='#666')
root.mainloop()
示例4: __init__
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def __init__(self, width=800, height=600):
root = Tk()
self.root = root
self.margin = 0.12*height
self.width, self.height = width, height - self.margin
self.cx, self.cy = width/2, (height - self.margin)/2
self.toolbar = \
Canvas(self.root, width=self.width, height=self.margin)
self.toolbar.pack()
self.canvas = Canvas(root, width=width, height=height - self.margin)
self.canvas.pack()
self.init_animation()
root.bind("<Button-1>", lambda e: self.mouse_event(e))
root.bind("<Key>", lambda e: self.key_event(e))
root.mainloop()
示例5: TkTimerCore
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
class TkTimerCore(TimerCore):
def __init__(self, *args, title, font_size, **kwargs):
def close_handler(event):
self.close()
def clicked_handler(event):
self.interact()
self.master = Tk()
self.master.wm_title(title)
self.master.bind('<Destroy>', close_handler)
self.label = Label(self.master, font='Sans {}'.format(int(font_size)))
self.label.pack(expand=True)
self.control = Toplevel()
self.control.wm_title(title + ' (control)')
self.control.minsize(150, 150)
self.control.bind('<Destroy>', close_handler)
self.button = Button(self.control, text='Start/Pause')
self.button.bind('<ButtonRelease>', clicked_handler)
self.button.pack(expand=True)
self.timeout_running = False
super().__init__(*args, **kwargs)
def start_timeout(self):
assert self.timeout_running is False
def timeout_call():
if self.timeout_running:
self.update()
self.master.after(25, timeout_call)
self.timeout_running = True
timeout_call()
def stop_timeout(self):
assert self.timeout_running is True
self.timeout_running = False
def mainloop(self):
return self.master.mainloop()
def shutdown(self):
self.master.quit()
def set_label_text(self, text, finished=False):
self.label.config(text=text)
if finished:
self.label.config(fg='red')
示例6: __init__
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
class Scratchpad:
def __init__(self):
self.window = Tk()
self.window.title("Onager Scratchpad")
self.create_frame()
self.create_editing_window()
self.window.bind('<F7>', self.save_file)
self.window.bind('<F5>', self.open_file)
def create_frame(self):
frame_style = Style()
frame_style.theme_use('alt')
frame_style.configure("TFrame",
relief='raised')
self.frame = Frame(self.window, style="frame_style.TFrame")
self.frame.rowconfigure(1)
self.frame.columnconfigure(1)
self.frame.grid(row=0, column=0)
def create_editing_window(self):
self.editing_window = ScrolledText(self.frame)
self.editing_window.configure(fg='gold',
bg='blue',
font='serif 12',
padx='15',
pady='15',
wrap='word',
borderwidth='10',
relief='sunken',
tabs='48',
insertbackground='cyan')
self.editing_window.grid(row=0, column=0)
def save_file(self, event=None):
name = asksaveasfilename()
outfile = open(name, 'w')
contents = self.editing_window.get(0.0, END)
outfile.write(contents)
outfile.close()
def open_file(self, event=None):
name = askopenfilename()
infile = open(name, 'r')
contents = infile.read()
self.editing_window.insert(INSERT, contents)
infile.close()
示例7: main
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def main():
getConfig()
root = Tk()
ents = makeform(root, fields)
root.bind('<Return>', (lambda event, e=ents: fetch(e)))
b1 = Button(root, text='Login',
command=(lambda e=ents: fetch(e)))
b1.pack(side=LEFT, padx=5, pady=5)
# b2 = Button(root, text='Logout',
# command=(lambda e=ents: fetch(e)))
# b2.pack(side=LEFT, padx=5, pady=5)
b3 = Button(root, text='Quit', command=root.quit)
b3.pack(side=LEFT, padx=5, pady=5)
root.mainloop()
示例8: download_track
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def download_track():
def callback(*args):
global notes
if len(track_id.get()):
r = session["session"].get(session["url"]+"download/"+track_id.get())
notes = json.loads(r.text)
root.destroy()
root = Tk()
root.title("Download")
root.geometry("300x90")
track_id = Entry(root, bd = 5, width=20)
track_id.focus()
track_id.pack()
button = Tk_button(root, text="Download", width=30, command=callback)
button.pack()
root.bind("<Return>", callback)
root.mainloop()
示例9: main
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def main():
def doneShaking(msg):
print(msg)
def keyboard(event):
shakeit.shake(window, 10, 10, None, doneShaking, True, "Done Shaking :)")
game.keyboard(event)
def mouse_b1(event):
game.mouse_b1(event)
window = Tk()
game = Game(window, 640, 480)
window.bind('<Key>', keyboard)
window.bind('<Button-1>', mouse_b1)
game.draw()
from database import Database as db
game.hexagons[2].setEntity(db.enemies[0].clone(1))
window.mainloop()
示例10: check_login
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def check_login():
def callback(*args):
if login(user.get(), password.get()) == 0:
session["auth"] = 1
session["username"] = user.get()
root.destroy()
root = Tk()
root.title("Log In")
root.geometry("300x90")
user = Entry(root, bd = 5, width=40)
user.focus()
user.pack()
password = Entry(root, bd = 5, show="*", width=40)
password.pack()
button = Tk_button(root, text="Log In", width=30, command=callback)
button.pack()
root.bind("<Return>", callback)
root.mainloop()
示例11: upload_track
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def upload_track():
def callback(*args):
r = session["session"].get(session["url"]+"upload")
i = r.text.find("csrfmiddlewaretoken")
session["csrfmiddlewaretoken"] = r.text[i+28:i+28+32] #Грязные хаки
data = json.dumps(notes)#.encode()
files = {"source": ("{}_{}.json".format(session["username"], track_name.get()), data, 'application/octet-stream', {'Expires': '0'})}
r = session["session"].post(session["url"]+"upload", files=files, data={"csrfmiddlewaretoken": session["csrfmiddlewaretoken"]})
root.destroy()
root = Tk()
root.title("Upload")
root.geometry("300x90")
track_name = Entry(root, bd = 5, width=40)
track_name.focus()
track_name.pack()
button = Tk_button(root, text="Upload", width=30, command=callback)
button.pack()
root.bind("<Return>", callback)
root.mainloop()
示例12: __init__
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def __init__(self, lineThickness=3, maxH=1920, maxW=1080):
master = Tk()
master.maxsize(maxH, maxW)
self.localCanvas = Canvas(master, width=400, height=400)
self.currentlyRenderedWindow = None
self.lineThickness = lineThickness
self.vsb = Scrollbar(master, orient="vertical", command=self.localCanvas.yview)
self.hsb = Scrollbar(master, orient="horizontal", command=self.localCanvas.xview)
self.localCanvas.configure(yscrollcommand=self.vsb.set)
self.localCanvas.configure(xscrollcommand=self.hsb.set)
master.bind("<Configure>", self.__eventOnFrameConfigure)
self.hsb.pack(side="bottom", fill="x")
self.vsb.pack(side="right", fill="y")
self.localCanvas.pack()
self.__sampleDraw()
''''''
示例13: demo3
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def demo3():
from nltk import Production
(S, VP, NP, PP, P, N, Name, V, Det) = \
nonterminals('S, VP, NP, PP, P, N, Name, V, Det')
productions = (
# Syntactic Productions
Production(S, [NP, VP]),
Production(NP, [Det, N]),
Production(NP, [NP, PP]),
Production(VP, [VP, PP]),
Production(VP, [V, NP, PP]),
Production(VP, [V, NP]),
Production(PP, [P, NP]),
Production(PP, []),
Production(PP, ['up', 'over', NP]),
# Lexical Productions
Production(NP, ['I']), Production(Det, ['the']),
Production(Det, ['a']), Production(N, ['man']),
Production(V, ['saw']), Production(P, ['in']),
Production(P, ['with']), Production(N, ['park']),
Production(N, ['dog']), Production(N, ['statue']),
Production(Det, ['my']),
)
t = Tk()
def destroy(e, t=t): t.destroy()
t.bind('q', destroy)
p = ProductionList(t, productions)
p.pack(expand=1, fill='both')
p.add_callback('select', p.markonly)
p.add_callback('move', p.markonly)
p.focus()
p.mark(productions[2])
p.mark(productions[8])
示例14: main
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def main(sumocfg="racing/racing.sumocfg", egoID="ego"):
root = Tk()
root.geometry('180x100+0+0')
frame = Frame(root)
Button(frame, text="Click here.\nControl with arrow keys").grid(row=0)
root.bind('<Left>', leftKey)
root.bind('<Right>', rightKey)
root.bind('<Up>', upKey)
root.bind('<Down>', downKey)
root.winfo_screenwidth()
root.winfo_screenheight()
frame.pack()
RacingClient(root, sumocfg, egoID)
root.mainloop()
示例15: main
# 需要导入模块: from tkinter import Tk [as 别名]
# 或者: from tkinter.Tk import bind [as 别名]
def main():
"""Main function"""
window = Tk()
window.title("McGyver Escape Game")
canvas = Canvas(window, width=300, height=300)
canvas.pack()
mapg = Map("./resources/map.txt", canvas)
mapg.load_map()
mapg.paint_map()
window.bind("<Up>", lambda event, sens='N': mapg.move_macgyver(sens))
window.bind("<Right>", lambda event, sens='E': mapg.move_macgyver(sens))
window.bind("<Down>", lambda event, sens='S': mapg.move_macgyver(sens))
window.bind("<Left>", lambda event, sens='W': mapg.move_macgyver(sens))
window.mainloop()