Tkinter解除绑定关键事件问题

时间:2020-10-29 11:45:28

标签: python python-3.x windows tkinter

在下面的代码中,按两次空格键会导致连续两次发出哔声。我想避免这种情况,而是在第一次发出哔声时禁用该键。我以为取消绑定空格键可能有效,但事实并非如此。奇怪的是,只有两声蜂鸣声似乎堆积而不是更多。我猜可能是Winsound的原因。哔声没有阻塞,因此重新绑定几乎立即发生。

请问有关如何使其正常工作的任何建议?

import winsound
from tkinter import *

             
def beep(e):
    frame.unbind("<space>")
    winsound.Beep(440, 1000)
    frame.bind("<space>", beep)


root = Tk()
frame = Frame(root, width=100, height=100)
frame.bind("<space>", beep)
frame.pack()
frame.focus_set()
root.mainloop()

4 个答案:

答案 0 :(得分:2)

这是一种将焦点从小部件上移开的解决方案,因此不会触发绑定:

import winsound
from tkinter import *

def beep(event):
    dummy.focus_set() #setting focus to dummy
    winsound.Beep(440, 1000) #playing it 
    root.after(1000,frame.focus_set) #setting focus back after playing for 1000 ms

root = Tk()

dummy = Label() #making a dummy widget
dummy.pack()
frame = Frame(root, width=100, height=100)
frame.bind("<space>",beep)
frame.pack()
frame.focus_set()

root.mainloop()

我评论它是为了更好地理解,但这只是一种解决方法,理解也不是那么复杂。

还请记住,在所有使用winsound的情况下,只要蜂鸣声已经开始并结束播放,GUI都将无响应,也就是说,GUI将在1秒钟内无响应(在您的情况下) )。

答案 1 :(得分:1)

这应该可以解决,但是您必须使用keyboard下载pip install keyboard模块:

import winsound
from tkinter import *
import keyboard
from _thread import start_new_thread

def beep():
    while True:
        if keyboard.is_pressed('space'):
            winsound.Beep(440, 1000)

root = Tk()

frame = Frame(root, width=100, height=100)
start_new_thread(beep, ())
frame.pack()
frame.focus_set()

root.mainloop()

第一个start_new_thread()(语法很重要)使beep()成为线程(在后台运行?),并使其while循环,从而使其连续运行,并且每当您按下空格键时,{{1 }},即使您发送垃圾邮件,它也只会运行一个beep。但是有一个缺点。它会在脚本未终止的情况下运行,因此如果您集中注意力,则按空格键仍会发出蜂鸣声

答案 2 :(得分:1)

您可以使用自上次成功[INFO] com.testapp.appname:appname-api:jar:4.3.5 [INFO] +- com.testapp.toolkit:testapp-toolkit-security-web:jar:5.0.0:compile [INFO] | +- org.springframework.boot:spring-boot-starter-oauth2-client:jar:2.3.4.RELEASE:compile [INFO] | | +- com.sun.mail:jakarta.mail:jar:1.6.5:compile [INFO] | | +- org.springframework.security:spring-security-oauth2-client:jar:5.3.4.RELEASE:compile [INFO] | | | \- org.springframework.security:spring-security-oauth2-core:jar:5.3.4.RELEASE:compile [INFO] | | \- org.springframework.security:spring-security-oauth2-jose:jar:5.3.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-oauth2-resource-server:jar:2.3.4.RELEASE:compile [INFO] | | \- org.springframework.security:spring-security-oauth2-resource-server:jar:5.3.4.RELEASE:compile [INFO] | \- com.testapp.toolkit:testapp-toolkit-utils:jar:5.0.0:compile [INFO] | +- commons-io:commons-io:jar:2.8.0:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.11.2:compile [INFO] | +- javax.validation:validation-api:jar:2.0.1.Final:compile [INFO] | +- org.rauschig:jarchivelib:jar:1.1.0:compile [INFO] | +- io.jsonwebtoken:jjwt:jar:0.9.1:compile [INFO] | +- org.springframework.boot:spring-boot-starter-cache:jar:2.3.4.RELEASE:compile [INFO] | | \- org.springframework:spring-context-support:jar:5.2.9.RELEASE:compile [INFO] | +- com.github.ben-manes.caffeine:caffeine:jar:2.8.5:compile [INFO] | | +- org.checkerframework:checker-qual:jar:3.4.1:compile [INFO] | | \- com.google.errorprone:error_prone_annotations:jar:2.4.0:compile [INFO] | +- nl.garvelink.oss:iban:jar:1.6.1:compile [INFO] | +- com.neovisionaries:nv-i18n:jar:1.27:compile [INFO] | +- fr.marcwrobel:jbanking:jar:2.1.0:compile [INFO] | +- org.springframework.boot:spring-boot-starter-thymeleaf:jar:2.3.4.RELEASE:compile [INFO] | | +- org.thymeleaf:thymeleaf-spring5:jar:3.0.11.RELEASE:compile [INFO] | | \- org.thymeleaf.extras:thymeleaf-extras-java8time:jar:3.0.4.RELEASE:compile [INFO] | +- nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:2.4.1:compile [INFO] | | +- nz.net.ultraq.thymeleaf:thymeleaf-expression-processor:jar:1.1.3:compile [INFO] | | +- org.codehaus.groovy:groovy:jar:2.5.13:compile [INFO] | | \- org.thymeleaf:thymeleaf:jar:3.0.11.RELEASE:compile [INFO] | | +- ognl:ognl:jar:3.1.12:compile [INFO] | | +- org.attoparser:attoparser:jar:2.0.5.RELEASE:compile [INFO] | | \- org.unbescape:unbescape:jar:1.1.6.RELEASE:compile [INFO] | +- org.bouncycastle:bcmail-jdk15on:jar:1.66:compile [INFO] | | +- org.bouncycastle:bcprov-jdk15on:jar:1.66:compile [INFO] | | \- org.bouncycastle:bcpkix-jdk15on:jar:1.66:compile [INFO] | \- com.microsoft.alm:auth-secure-storage:jar:0.6.4:compile [INFO] | +- com.microsoft.alm:auth-common:jar:0.6.4:compile [INFO] | \- com.microsoft.alm:oauth2-useragent:jar:0.11.3:compile [INFO] +- com.testapp.toolkit:testapp-toolkit-security:jar:5.0.0:compile [INFO] | +- org.projectlombok:lombok:jar:1.18.12:compile [INFO] | \- com.github.spotbugs:spotbugs-annotations:jar:4.1.2:compile [INFO] | \- com.google.code.findbugs:jsr305:jar:3.0.2:compile [INFO] +- com.testapp.toolkit:testapp-toolkit-iso20022-model:jar:5.0.0:compile [INFO] | \- com.sun.xml.bind:jaxb-impl:jar:2.3.3:compile [INFO] | \- com.sun.activation:jakarta.activation:jar:1.2.2:compile [INFO] +- com.testapp.test3:test3:jar:4.3.3:compile [INFO] | +- com.testapp.toolkit:testapp-toolkit-content-validation:jar:5.0.0:compile [INFO] | | +- org.apache.tika:tika-core:jar:1.20:compile [INFO] | | +- org.apache.tika:tika-parsers:jar:1.20:compile [INFO] | | | +- com.sun.activation:javax.activation:jar:1.2.0:compile [INFO] | | | +- org.tallison:jmatio:jar:1.5:compile [INFO] | | | +- org.apache.james:apache-mime4j-core:jar:0.8.2:compile [INFO] | | | +- org.apache.james:apache-mime4j-dom:jar:0.8.2:compile [INFO] | | | +- org.tukaani:xz:jar:1.8:compile [INFO] | | | +- com.epam:parso:jar:2.0.10:compile [INFO] | | | +- org.brotli:dec:jar:0.1.2:compile [INFO] | | | +- org.apache.pdfbox:pdfbox:jar:2.0.13:compile [INFO] | | | | \- org.apache.pdfbox:fontbox:jar:2.0.13:compile [INFO] | | | +- org.apache.pdfbox:pdfbox-tools:jar:2.0.13:compile [INFO] | | | +- org.apache.pdfbox:jempbox:jar:1.8.16:compile [INFO] | | | +- com.healthmarketscience.jackcess:jackcess-encrypt:jar:2.1.4:compile [INFO] | | | +- org.ccil.cowan.tagsoup:tagsoup:jar:1.2.1:compile [INFO] | | | +- com.googlecode.mp4parser:isoparser:jar:1.1.22:compile [INFO] | | | +- com.drewnoakes:metadata-extractor:jar:2.11.0:compile [INFO] | | | | \- com.adobe.xmp:xmpcore:jar:5.1.3:compile [INFO] | | | +- de.l3s.boilerpipe:boilerpipe:jar:1.1.0:compile [INFO] | | | +- com.rometools:rome:jar:1.12.0:compile [INFO] | | | | \- com.rometools:rome-utils:jar:1.12.0:compile [INFO] | | | +- org.gagravarr:vorbis-java-core:jar:0.8:compile [INFO] | | | +- com.googlecode.juniversalchardet:juniversalchardet:jar:1.0.3:compile [INFO] | | | +- org.codelibs:jhighlight:jar:1.0.3:compile [INFO] | | | +- com.pff:java-libpst:jar:0.8.1:compile [INFO] | | | +- com.github.junrar:junrar:jar:2.0.0:compile [INFO] | | | +- org.apache.commons:commons-exec:jar:1.3:compile [INFO] | | | +- org.apache.opennlp:opennlp-tools:jar:1.9.0:compile [INFO] | | | +- com.googlecode.json-simple:json-simple:jar:1.1.1:compile [INFO] | | | +- com.github.openjson:openjson:jar:1.0.10:compile [INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.30:compile [INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.30:compile [INFO] | | | +- edu.ucar:netcdf4:jar:4.5.5:compile [INFO] | | | | \- net.jcip:jcip-annotations:jar:1.0:compile [INFO] | | | +- edu.ucar:grib:jar:4.5.5:compile [INFO] | | | | \- org.itadaki:bzip2:jar:0.9.1:compile [INFO] | | | +- org.jsoup:jsoup:jar:1.11.3:compile [INFO] | | | +- edu.ucar:cdm:jar:4.5.5:compile [INFO] | | | | +- edu.ucar:udunits:jar:4.5.5:compile [INFO] | | | | +- joda-time:joda-time:jar:2.2:compile [INFO] | | | | +- net.sf.ehcache:ehcache-core:jar:2.6.2:compile [INFO] | | | | \- com.beust:jcommander:jar:1.35:compile [INFO] | | | +- edu.ucar:httpservices:jar:4.5.5:compile [INFO] | | | +- org.apache.httpcomponents:httpmime:jar:4.5.12:compile [INFO] | | | +- org.apache.commons:commons-csv:jar:1.6:compile [INFO] | | | +- org.apache.sis.core:sis-utility:jar:0.8:compile [INFO] | | | | \- javax.measure:unit-api:jar:1.0:compile [INFO] | | | +- org.apache.sis.storage:sis-netcdf:jar:0.8:compile [INFO] | | | | +- org.apache.sis.storage:sis-storage:jar:0.8:compile [INFO] | | | | | \- org.apache.sis.core:sis-feature:jar:0.8:compile [INFO] | | | | \- org.apache.sis.core:sis-referencing:jar:0.8:compile [INFO] | | | +- org.apache.sis.core:sis-metadata:jar:0.8:compile [INFO] | | | +- org.opengis:geoapi:jar:3.0.1:compile [INFO] | | | +- org.apache.uima:uimafit-core:jar:2.4.0:compile [INFO] | | | +- org.apache.uima:uimaj-core:jar:3.0.1:compile [INFO] | | | | +- org.bitbucket.mstrobel:procyon-compilertools:jar:0.5.32:compile [INFO] | | | | +- org.bitbucket.mstrobel:procyon-core:jar:0.5.32:compile [INFO] | | | | \- com.sun.xml.bind:jaxb-core:jar:2.3.0.1:runtime [INFO] | | | +- org.jdom:jdom2:jar:2.0.6:compile [INFO] | | | \- org.apache.pdfbox:jbig2-imageio:jar:3.0.2:compile [INFO] | | +- com.google.protobuf:protobuf-java:jar:3.13.0:compile [INFO] | | +- org.apache.poi:poi-scratchpad:jar:4.0.0:compile [INFO] | | \- org.apache.commons:commons-compress:jar:1.20:compile [INFO] | +- org.springframework.security:spring-security-core:jar:5.3.4.RELEASE:compile [INFO] | | +- org.springframework:spring-beans:jar:5.2.9.RELEASE:compile [INFO] | | +- org.springframework:spring-context:jar:5.2.9.RELEASE:compile [INFO] | | \- org.springframework:spring-expression:jar:5.2.9.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-batch:jar:2.3.4.RELEASE:compile [INFO] | | \- org.springframework.batch:spring-batch-core:jar:4.2.4.RELEASE:compile [INFO] | | +- javax.batch:javax.batch-api:jar:1.0:compile [INFO] | | +- org.codehaus.jettison:jettison:jar:1.2:compile [INFO] | | \- org.springframework.batch:spring-batch-infrastructure:jar:4.2.4.RELEASE:compile [INFO] | | \- org.springframework.retry:spring-retry:jar:1.2.5.RELEASE:compile [INFO] | +- org.springframework:spring-oxm:jar:5.2.9.RELEASE:compile [INFO] | +- com.tracegroup.transformer:transformer-camel:jar:3.7.5-osgi-bundle:compile [INFO] | \- com.tracegroup.transformer:currencylib:jar:1.0.12:compile [INFO] +- com.testapp.toolkit:testapp-toolkit-rest-client:jar:5.0.0:compile [INFO] | +- io.swagger:swagger-annotations:jar:1.5.0:compile [INFO] | \- org.apache.commons:commons-collections4:jar:4.4:compile [INFO] +- com.testapp.toolkit:testapp-toolkit-application-properties:jar:5.0.0:compile [INFO] +- com.fts.test2:test2-sdk:jar:4.3.3:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.3.4.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.3.4.RELEASE:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.3.4.RELEASE:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile [INFO] | | | \- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile [INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile [INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.26:compile [INFO] | +- org.springframework:spring-web:jar:5.2.9.RELEASE:compile [INFO] | +- org.apache.httpcomponents:httpclient:jar:4.5.12:compile [INFO] | | \- org.apache.httpcomponents:httpcore:jar:4.4.13:compile [INFO] | +- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.11.2:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.11.2:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-core:jar:2.11.2:compile [INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:compile [INFO] | \- com.google.code.gson:gson:jar:2.8.6:compile [INFO] +- com.testapp.efm:efm- :jar:4.3.5:compile [INFO] | +- org.apache.commons:commons-lang3:jar:3.10:compile [INFO] | +- org.apache.poi:poi:jar:4.0.0:compile [INFO] | +- org.apache.poi:poi-ooxml:jar:4.0.0:compile [INFO] | | +- org.apache.poi:poi-ooxml-schemas:jar:4.0.0:compile [INFO] | | | \- org.apache.xmlbeans:xmlbeans:jar:3.1.0:compile [INFO] | | \- com.github.virtuald:curvesapi:jar:1.04:compile [INFO] | \- commons-lang:commons-lang:jar:2.6:compile [INFO] +- com.testapp.dbe:dbe:jar:4.3.1:compile [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.3.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.3.4.RELEASE:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.11.2:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.11.2:compile [INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.11.2:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.3.4.RELEASE:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.38:compile [INFO] | | +- org.glassfish:jakarta.el:jar:3.0.3:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.38:compile [INFO] | \- org.springframework:spring-webmvc:jar:5.2.9.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.3.4.RELEASE:test [INFO] | +- org.springframework.boot:spring-boot-test:jar:2.3.4.RELEASE:test [INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.3.4.RELEASE:test [INFO] | +- com.jayway.jsonpath:json-path:jar:2.4.0:test [INFO] | | \- net.minidev:json-smart:jar:2.3:compile [INFO] | | \- net.minidev:accessors-smart:jar:1.2:compile [INFO] | | \- org.ow2.asm:asm:jar:5.0.4:compile [INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:compile [INFO] | +- org.assertj:assertj-core:jar:3.16.1:test [INFO] | +- org.hamcrest:hamcrest:jar:2.2:test [INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.6.2:test [INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.6.2:test [INFO] | | | +- org.opentest4j:opentest4j:jar:1.2.0:test [INFO] | | | \- org.junit.platform:junit-platform-commons:jar:1.6.2:test [INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.6.2:test [INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.6.2:test [INFO] | +- org.junit.vintage:junit-vintage-engine:jar:5.6.2:test [INFO] | | +- org.apiguardian:apiguardian-api:jar:1.1.0:test [INFO] | | +- org.junit.platform:junit-platform-engine:jar:1.6.2:test [INFO] | | \- junit:junit:jar:4.13:test [INFO] | +- org.mockito:mockito-core:jar:3.3.3:test [INFO] | | +- net.bytebuddy:byte-buddy:jar:1.10.14:compile [INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.10.14:test [INFO] | | \- org.objenesis:objenesis:jar:2.6:test [INFO] | +- org.mockito:mockito-junit-jupiter:jar:3.3.3:test [INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test [INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:runtime [INFO] | +- org.springframework:spring-core:jar:5.2.9.RELEASE:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.2.9.RELEASE:compile [INFO] | +- org.springframework:spring-test:jar:5.2.9.RELEASE:test [INFO] | \- org.xmlunit:xmlunit-core:jar:2.7.0:test [INFO] +- org.springframework.boot:spring-boot-starter-security:jar:2.3.4.RELEASE:compile [INFO] | +- org.springframework:spring-aop:jar:5.2.9.RELEASE:compile [INFO] | +- org.springframework.security:spring-security-config:jar:5.3.4.RELEASE:compile [INFO] | \- org.springframework.security:spring-security-web:jar:5.3.4.RELEASE:compile [INFO] +- org.springframework.security:spring-security-test:jar:5.3.4.RELEASE:test [INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.3.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:2.3.4.RELEASE:compile [INFO] | | \- org.aspectj:aspectjweaver:jar:1.9.6:compile [INFO] | +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.3.4.RELEASE:compile [INFO] | | +- com.zaxxer:HikariCP:jar:3.4.5:compile [INFO] | | \- org.springframework:spring-jdbc:jar:5.2.9.RELEASE:compile [INFO] | +- jakarta.transaction:jakarta.transaction-api:jar:1.3.3:compile [INFO] | +- jakarta.persistence:jakarta.persistence-api:jar:2.2.3:compile [INFO] | +- org.hibernate:hibernate-core:jar:5.4.21.Final:compile [INFO] | | +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile [INFO] | | +- org.javassist:javassist:jar:3.24.0-GA:compile [INFO] | | +- antlr:antlr:jar:2.7.7:compile [INFO] | | +- org.jboss:jandex:jar:2.1.3.Final:compile [INFO] | | +- com.fasterxml:classmate:jar:1.5.1:compile [INFO] | | +- org.dom4j:dom4j:jar:2.1.3:compile [INFO] | | +- org.hibernate.common:hibernate-commons-annotations:jar:5.1.0.Final:compile [INFO] | | \- org.glassfish.jaxb:jaxb-runtime:jar:2.3.3:compile [INFO] | | +- org.glassfish.jaxb:txw2:jar:2.3.3:compile [INFO] | | \- com.sun.istack:istack-commons-runtime:jar:3.0.11:compile [INFO] | +- org.springframework.data:spring-data-jpa:jar:2.3.4.RELEASE:compile [INFO] | | +- org.springframework.data:spring-data-commons:jar:2.3.4.RELEASE:compile [INFO] | | +- org.springframework:spring-orm:jar:5.2.9.RELEASE:compile [INFO] | | \- org.springframework:spring-tx:jar:5.2.9.RELEASE:compile [INFO] | \- org.springframework:spring-aspects:jar:5.2.9.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.3.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.3.4.RELEASE:compile [INFO] | | \- org.springframework.boot:spring-boot-actuator:jar:2.3.4.RELEASE:compile [INFO] | \- io.micrometer:micrometer-core:jar:1.5.5:compile [INFO] | +- org.hdrhistogram:HdrHistogram:jar:2.1.12:compile [INFO] | \- org.latencyutils:LatencyUtils:jar:2.0.3:runtime 起的经过时间来确定是否发出蜂鸣声。

可能是这样的:我无法使用winsound,因此我正在使用keypress功能来模仿哔声。您可以对此进行注释,并取消注释对os

的呼叫
winsound

答案 3 :(得分:1)

您可以通过after_idle()绑定事件:

def beep(e):
    e.widget.unbind('<space>')
    winsound.Beep(440, 1000)
    e.widget.after_idle(e.widget.bind, '<space>', beep)

解释:

传递到after_idle()的回调将在tkinter mainloop空闲时执行,即没有待处理的待处理作品/事件。因此,如果多次按下空格键,则第一次按下将触发beep(),其中tkinter取消绑定该事件,发出蜂鸣声,然后安排重新绑定。在beep()返回之后,tkinter继续处理待处理的任务,即处理其余的空格键事件(但是此时,没有绑定处于活动状态),然后执行after_idle Schedule任务,即重新绑定。