我正在使用PyQt5,我想根据用户在现有按钮上的点击来绘制文本。
文本直接显示在Qwidget上,我希望文本在单击按钮后立即显示。 怎么做?
我的代码是这样的:
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE DeriveFunctor #-}
module TestStateJLJ1 where
import Data.Functor.Identity
import Control.Monad
data LogS = LogS { stepS :: Int , logS :: [String] }
deriving (Show)
initLogS :: LogS
initLogS = LogS 1 []
newtype StateT s m a
= StateT { runStateT :: (s, LogS) -> m (Maybe a, s, LogS) }
deriving (Functor)
instance (Monad m) => Applicative (StateT s m) where
pure a = StateT $ \(s, l) -> return (Just a, s, l)
(<*>) = ap
instance (Monad m) => Monad (StateT s m) where
return = pure
m >>= k = StateT $ \(s, l) -> do
(maybeA, s', l') <- runStateT m (s, l)
case maybeA of
Nothing -> return (Nothing, s', l')
Just a -> runStateT (k a) (s', l' {stepS = 1 + stepS l'})
fail err = StateT $ \(s, l) -> return (Nothing, s, l { logS = err:logS l })
type State s = StateT s Identity
state :: (Monad m) => ((s, LogS) -> (Maybe a, s, LogS)) -> StateT s m a
state f = StateT $ return . f
答案 0 :(得分:1)
首先,将文本设置为空:
self.text = ""
然后,创建按钮单击事件很重要:
self.btn1 = QPushButton("Button 1", self)
self.btn1.clicked.connect(self.button_click)
通过单击按钮创建要调用的函数:
def button_click(self):
self.text = "Just For Test"
self.repaint()
重新绘制将刷新您的QPaint