Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 让我们点击文本框的文本按钮_Python_User Interface_Tkinter - Fatal编程技术网

Python 让我们点击文本框的文本按钮

Python 让我们点击文本框的文本按钮,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在尝试创建webbrowser,但tkinter有问题,这是代码,但它给我一个错误: File "C:\Users\Fulvio\Desktop\OpenBrowser.py", line 10, in callback b=b+x+"&oq="+x+"&gs_l=serp.3..0l10.131037.133187.0.133344.13.9.1.3.3.0.195.895.0j6.6.0....0...1c.1.52.serp..3.10.922.ssgKbcBr

我正在尝试创建webbrowser,但tkinter有问题,这是代码,但它给我一个错误:

  File "C:\Users\Fulvio\Desktop\OpenBrowser.py", line 10, in callback
  b=b+x+"&oq="+x+"&gs_l=serp.3..0l10.131037.133187.0.133344.13.9.1.3.3.0.195.895.0j6.6.0....0...1c.1.52.serp..3.10.922.ssgKbcBrPr0"
  NameError: global name 'x' is not defined
代码如下:

import webbrowser

from Tkinter import *

def retrieve_input():
    global x
    x = self.myText_Box.get("0.0", 'END-1c')

def callback():
    b = "https://www.google.it/search?q="
    b=b+x+"&oq="+x+"&gs_l=serp.3..0l10.131037.133187.0.133344.13.9.1.3.3.0.195.895.0j6.6.0....0...1c.1.52.serp..3.10.922.ssgKbcBrPr0"

    if self.myText_Box.get("0.0", END)=="text":
        webbrowser.open(b)

top = Tk()

myText_Box = Label(top, text="Cerca")

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

E1 = Entry(top, bd = 5)

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

MyButton1 = Button(top, text="Submit", width=10, command=callback)

MyButton1.grid(row=1, column=1)

top.mainloop()

很抱歉,如果代码不是div灰色,但我不知道如何在
回调中修复
的分配语法

移动到类中将使您能够使用类实例变量,而无需求助于全局x

除非有非常特殊的需要,否则避免使用
global
-s

from Tkinter import *                        # just re-shaped into a Class-mode
import webbrowser                            # no app.logic revisions here

class aTkGuiApplication():
    def __init__( self, master ):            # .INIT auto-exec'd method to setup <self>
        root            = master
        self.x          = 'init'             # .DEF <self>.x instance-variable
        self.myText_Box = Label(  root, ...  # .DEF <self>.myText_Box instance
        self.E1         = Entry(  root,
                                  bd = 5
                                  )
        self.MyButton1  = Button( root,
                                  text    = "Submit",
                                  width   = 10,
                                  command = callback
                                  )
        self.E1.grid(             row = 0, column = 1 )
        self.myText_Box.grid(     row = 0, column = 0 )
        self.MyButton1.grid(      row = 1, column = 1 )

    def callback( self ):                            # .GUI Event-<control-loop>
        self.b = "https://www.google.it/search?q="   # .SET <self>.b
        self.b = self.b + self.x + "&og=" + self.x + "...etc...etc..."

        if self.myText_Box.get( "0.0", END ) == "text":
            webbrowser.open( self.b )

    def retrieve_input():                               # Erratum: 
        self.x = self.myText_Box.get( "0.0", 'END-1c' ) # .SET <self>.x a new value ...

aRootUiWINDOW = Tk()                                    # .GUI <Tk()> context
aRootUiWINDOW.title(    'Web TkGUI... v0.0' )           # .SET w.[TITLE]
aRootUiWINDOW.geometry( '1200x600-50-50')               # .SET w.size ... 1200 x 600 [px] w.right-edge -50[px] left from screen-right, w.bottom-edge -50[px] up from screen-botton
aRootUiWINDOW.configure( background = 'black' )         # .SET w.bg
app = GuiApplication(    aRootUiWINDOW )                # .SET <app>
aRootUiWINDOW.lift()                                    # .SET visible
aRootUiWINDOW.mainloop()                                # .GUI controller
来自Tkinter import*#的
刚刚重新成形为类模式
在此处导入webbrowser#无应用程序逻辑修订
类aTkGuiApplication():
def uu init uu(self,master):#.init auto exec'd方法进行设置
根=主
self.x='init'#.DEF.x实例变量
self.myText_-Box=标签(根,…#.DEF.myText_-Box实例)
self.E1=条目(根、,
bd=5
)
self.MyButton1=按钮(根,
text=“提交”,
宽度=10,
命令=回调
)
self.E1.grid(行=0,列=1)
self.myText_Box.grid(行=0,列=0)
self.MyButton1.grid(行=1,列=1)
def回调(self):#.GUI事件-
self.b=”https://www.google.it/search?q=“#.集.b
self.b=self.b+self.x+“&og=“+self.x+”…等…”
如果self.myText_Box.get(“0.0”,END)=“text”:
网络浏览器打开(self.b)
def retrieve_input():#勘误表:
self.x=self.myText_Box.get(“0.0”,“END-1c”)#。设置.x一个新值。。。
aRootUiWINDOW=Tk()#.GUI上下文
aRootUiWINDOW.title('Web TkGUI…v0.0')#.设置为w.[title]
aRootUiWINDOW.几何体('1200x600-50-50')#。设置w.尺寸…1200 x 600[px]w.右边缘-屏幕右侧左侧50[px],w.底部边缘-屏幕底部上方50[px]
aRootUiWINDOW.configure(背景='black')#。设置w.bg
app=gui应用程序(aRootUiWINDOW)#.设置
aRootUiWINDOW.lift()#.设置为可见
aRootUiWINDOW.mainloop()#.GUI控制器

global x in callback()。我也同意避免使用global,我只是提供了一个问题的快速解决方案:)对不起,但是在self.x in callback中,我应该放什么?@user3597474请原谅复制/粘贴的剩余错误。更正。