생성 시 버튼 명령 실행
Tkinter에서 Button 인스턴스가 명령 매개변수로 초기화될 때 관련 기능은 다음과 같습니다. 버튼을 클릭하면 실행됩니다. 그러나 경우에 따라 예상 동작과 달리 버튼 생성 시 명령이 즉시 실행될 수도 있습니다.
코드 예제 분석
다음 코드를 고려하세요.
from Tkinter import * admin = Tk() def button(an): print(an) print('het') b = Button(admin, text='as', command=button('hey')) b.pack() mainloop()
문제 설명
이 코드를 실행하면 버튼이 제대로 작동하지 않는 것처럼 보입니다. 버튼 클릭 시 버튼 기능을 실행하는 것이 아니라 생성 즉시 'hey', 'het'을 출력합니다.
설명
문제는 명령을 실행하는 방식에 있습니다. 매개변수를 초기화하는 중입니다. 라인:
command=button('hey')
은 버튼('hey') 함수 호출의 결과를 명령 매개변수에 전달합니다. 이는 다음과 동일합니다:
result = button('hey') command=result
button('hey') 함수가 즉시 실행되어 결과를 반환하므로 명령은 함수 참조 자체가 아닌 결과로 설정됩니다. 결과적으로 버튼을 눌렀을 때가 아닌 바로 함수가 실행됩니다.
해결책
이 문제를 해결하려면 버튼에 대한 참조를 전달해야 합니다. 실행하지 않고 기능을 수행합니다. 이렇게 하려면 명령 매개변수를 초기화할 때 괄호를 생략하면 됩니다.
command=button
추가 참고 사항
- 함수 전달 간의 차이점을 이해하는 것이 중요합니다. 참조하고 함수를 실행합니다.
- 제공하려면 람다 함수나 functools.partial을 사용하는 것이 좋습니다. 필요할 때 콜백 함수에 대한 매개변수.
- 자세한 내용은 Tkinter 콜백에 대한 zone.effbot.org 튜토리얼을 참조하세요.
위 내용은 내 Tkinter 버튼이 클릭할 때 대신 생성할 때 명령을 실행하는 이유는 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

PythonuseSahybrideactroach, combingingcompytobytecodeandingretation.1) codeiscompiledToplatform-IndependentBecode.2) bytecodeistredbythepythonvirtonmachine, enterancingefficiency andportability.

"for"and "while"loopsare : 1) "에 대한"loopsareIdealforitertatingOverSorkNowniterations, whide2) "weekepindiTeRations.Un

Python에서는 다양한 방법을 통해 목록을 연결하고 중복 요소를 관리 할 수 있습니다. 1) 연산자를 사용하거나 ()을 사용하여 모든 중복 요소를 유지합니다. 2) 세트로 변환 한 다음 모든 중복 요소를 제거하기 위해 목록으로 돌아가지 만 원래 순서는 손실됩니다. 3) 루프 또는 목록 이해를 사용하여 세트를 결합하여 중복 요소를 제거하고 원래 순서를 유지하십시오.

fastestestestedforListCancatenationInpythondSpendsonListsize : 1) Forsmalllist, OperatoriseFficient.2) ForlargerLists, list.extend () OrlistComprehensionIsfaster, withextend () morememory-efficientBymodingListsin-splace.

toInsertElmentsIntoapyThonList, useAppend () toaddtotheend, insert () foraspecificposition, andextend () andextend () formultipleElements.1) useappend () foraddingsingleitemstotheend.2) useinsert () toaddatespecificindex, 그러나)

pythonlistsareimplementedesdynamicarrays, notlinkedlists.1) thearestoredIntIguousUousUousUousUousUousUousUousUousUousInSeripendExeDaccess, LeadingSpyTHOCESS, ImpactingEperformance

PythonoffersfourmainmethodstoremoveElementsfromalist : 1) 제거 (값) 제거 (값) removesthefirstoccurrencefavalue, 2) pop (index) 제거 elementatAspecifiedIndex, 3) delstatemeveselementsByindexorSlice, 4) RemovesAllestemsfromTheChmetho

Toresolvea "permissionDenied"오류가 발생할 때 오류가 발생합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전