【问题标题】:how to check the button works如何检查按钮是否有效
【发布时间】:2021-11-30 10:27:15
【问题描述】:

我知道“ip”有效,但是当我点击按钮时,我什么也看不到(用户应该什么也看不到,所以这很棒!)但是,我不知道如何验证按钮的功能。 目标是最终将按钮的指定信息发送到标签小部件(用户将看到)

import tkinter as tk
import subprocess


ip = subprocess.run("ipconfig /all",universal_newlines=True,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)


#Setup for the window
window =tk.Tk()
window.title("Title_Name")
window.geometry("300x500")
x = window.winfo_x()
y = window.winfo_y()
sh = window.winfo_screenheight()
sw = window.winfo_screenwidth()
x_cord = int((sw/2)-550)
y_cord = int((sh/2)-300)
wh = 350
ww = 500
window.geometry("{}x{}+{}+{}".format(ww, wh ,x_cord, y_cord))



#widgets
button = tk.Button(window, text= "submit",
                   command = ip)

#widget placement
button.grid(row=0, column=0, padx=5)

【问题讨论】:

  • 您正在运行ipconfig 命令一次,因为您的程序正在启动。 subprocess.run 的返回值不是可以作为 Button 的 command= 选项传递的函数。
  • @jasonharper 是否可以在我有用户输入的地方执行 subprocess.run("Input_goes_here") 来决定运行什么代码,或者是将“ip”转换为函数并调用它的唯一选择根据需要?

标签: python tkinter button


【解决方案1】:

只需快速将命令更改为不同的函数来测试命令!

import tkinter as tk
import subprocess


ip = subprocess.run("ipconfig /all",universal_newlines=True,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)


#Setup for the window
window =tk.Tk()
window.title("Title_Name")
window.geometry("300x500")
x = window.winfo_x()
y = window.winfo_y()
sh = window.winfo_screenheight()
sw = window.winfo_screenwidth()
x_cord = int((sw/2)-550)
y_cord = int((sh/2)-300)
wh = 350
ww = 500
window.geometry("{}x{}+{}+{}".format(ww, wh ,x_cord, y_cord))

def ButtonTest():
   print('The button is working!')

#widgets
button = tk.Button(window, text= "submit",
                   command = ButtonTest)

#widget placement
button.grid(row=0, column=0, padx=5)

tk.mainloop()

当您运行此代码并按下按钮时,它将打印输出,因此您可以放心,假设 ip 函数正在运行,它将运行!

【讨论】:

  • 现在我觉得我只是想多了。哈哈,谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 2010-12-20
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多