python按鈕的響應事件 如何使我的按鈕響應單擊

2021-10-13 03:22:55 字數 1383 閱讀 2263

1-你從不打**給你的gui

2-在函式內定義的變數具有區域性作用域,不能在外部訪問它們,除非您在全域性範圍內宣告它們,並使用global來更改它們的值。在

以下內容符合您的預期:

如果沒有選中單選按鈕,click將列印0

選擇「紅色」單選按鈕後,click列印1

選擇「黃色」按鈕後,click列印2

可以使用這些值將呼叫重定向到另乙個函式。在import tkinter as tk

def do_nothing():

pass

def do_red():

print('doing the red thing')

def do_yellow():

print('doing the yellow thing')

def clicked():

print(f'clicked ')

v = selected.get()

if v == 0:

do_nothing()

elif v == 1:

do_red()

elif v == 2:

do_yellow()

else:

print('an error occurred, the wrong value was recieved')

def gui():

global rad1, rad2, selected

window = tk.tk()

window.title("select first color")

selected = tk.intvar()

rad1 = tk.radiobutton(window, text='red', value=1, variable=selected)

rad2 = tk.radiobutton(window, text='yellow', value=2, variable=selected)

button1 = tk.button(window, text="select", command=clicked)

button2 = tk.button(window, text="quit", command=window.destroy)

rad1.grid(column=0, row=0)

rad2.grid(column=1, row=0)

button1.grid(column=6, row=0)

button2.grid(column=6, row=1)

window.mainloop()

rad1, rad2, selected = none, none, none

gui()

或者,可以使用類來避免全域性變數:

^$

DuiLib 響應按鈕事件

pragma once duiib 響應按鈕事件 author 邱于涵 2016年12月16日18 25 20 include duilb的標頭檔案 include duilib的命名空間 using namespace duilib 根據巨集判斷語句 判斷是否unicode 以及相應的動態鏈結庫 ...

Button按鈕響應事件的2種實現方式

方法一 在oncreate中新增如下 button button ok button findviewbyid r.id.button ok button button exit button findviewbyid r.id.button exit button ok.setonclicklis...

如何正確響應安全事件?

在當今這個資訊科技高度發達的時代,每乙個組織和機構都要時刻準備著與資訊保安威脅作鬥爭。在這場沒有硝煙的資訊化戰爭中,企業或組織不應該孤身奮戰,而是應該與某些安全技術合作,以共同應對日趨複雜的安全風險。這將關係到自身網路系統的完整性,所以其重要性不言而喻。你可能會認為,你們公司已經部署了應對計算機病毒...