【问题标题】:How to make a small Button in Python without Tkinter如何在没有 Tkinter 的情况下在 Python 中制作一个小按钮
【发布时间】:2021-10-14 13:50:10
【问题描述】:

在 Python 中不使用 tkinter,有人可以帮忙吗? 这是我想添加的代码:

import time

pswrd = '12345678'

while True:

 m = input("1.Login   2.Reset Password\n")
 
 if m == "1":
     
     n = input("Password:\n")

     if n == pswrd:
       print("Password Correct\n")
   
     else:
         print("Wrong Password\n")
     
 else:
     n = input("New Password:\n")
     pswrd = n
     repeat = 'While True'

我开始为我想要构建的操作系统进行一些测试,我知道,这是一个很大的目标,但是是的......它只是帮助我训练我的新手技能 x) 有人可以帮忙吗?

【问题讨论】:

  • 您希望按钮显示在哪里?作为什么窗口的孩子?按钮必须在某处。 Tkinter 和其他 UI 库为您提供了一个地方。
  • @Kemp 紧跟在 if n == pswrd: print("Password Correct\n")
  • 我的意思是从字面上看在屏幕上,按钮应该显示在哪里?终端窗口不能显示与文本内联的图形按钮,那么按钮出现在哪里?你在哪里实现事件循环来监听被点击的按钮?许多基础设施都用于显示 UI 项目并与之交互。

标签: python button


【解决方案1】:

使用 python Kivy 或 python pyQT。

例子:

带有 Kivy 的按钮:

# import kivy module
import kivy

# this restrict the kivy version i.e
# below this kivy version you cannot
# use the app or software
kivy.require("1.9.1")

# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App

# creates the button in kivy
# if not imported shows the error
from kivy.uix.button import Button

# class in which we are creating the button
class ButtonApp(App):
    
    def build(self):
        # use a (r, g, b, a) tuple
        btn = Button(text ="Push Me !",
                font_size ="20sp",
                background_color =(1, 1, 1, 1),
                color =(1, 1, 1, 1),
                size =(32, 32),
                size_hint =(.2, .2),
                pos =(300, 250))

        # bind() use to bind the button to function callback
        btn.bind(on_press = self.callback)
        return btn

    # callback function tells when button pressed
    def callback(self, event):
        print("button pressed")
        print('Yoooo !!!!!!!!!!!')
        

# creating the object root for ButtonApp() class
root = ButtonApp()

# run function runs the whole program
# i.e run() method which calls the target
# function passed to the constructor.
root.run()

带有 pQT 的按钮:

# importing libraries
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        # setting title
        self.setWindowTitle("Python ")

        # setting geometry
        self.setGeometry(100, 100, 600, 400)

        # calling method
        self.UiComponents()

        # showing all the widgets
        self.show()

    # method for widgets
    def UiComponents(self):

        # creating a push button
        button = QPushButton("CLICK", self)

        # setting geometry of button
        button.setGeometry(200, 150, 100, 30)

        # adding action to a button
        button.clicked.connect(self.clickme)

    # action method
    def clickme(self):

        # printing pressed
        print("pressed")

# create pyqt5 app
App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# start the app
sys.exit(App.exec())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-18
    • 2021-09-20
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多