Page 1 of 1

Re: tkinter How to pass entry to another module?

Posted: Mon Oct 23, 2017 1:41 pm
by scotty101
At the moment the first code listing ( does not include or call anything in the other program listing but the other program imports server_channel. I'd do this the other way round.

Inside do the following things
import irc_connection
create an instance of the IRC_Server class
open gui.
When the enter button is pressed, call a function which calls a method of the IRC_Server instance passing the value of the entry box as a parameter.

I'll try to create a simple example and post it here.

Re: tkinter How to pass entry to another module?

Posted: Mon Oct 23, 2017 1:59 pm
by scotty101
Ok a simple dumbed down example so I am not doing all the work for you.

Code: Select all

#!/usr/bin/python 3.6
from tkinter import *
from chat_client import ChatClient

def server_get(event):
    text = entry.get()

client = ChatClient()

root = Tk()
v = StringVar()
text1 = Text(root, height=25, width=125, bg='black', fg='yellow')
scroll = Scrollbar(root, command=text1.yview, orient=VERTICAL)
entry = Entry(root, textvariable = v, bg='white', fg='black')
entry.pack(side = BOTTOM, fill=BOTH)
text1.pack(side=LEFT, fill=BOTH, expand='YES')
scroll.pack(side=RIGHT, fill=BOTH)
entry.bind('<Return>', server_get)


Code: Select all

class ChatClient:
     def __init__(self):
        print('Created Chat Client')
     def connect(self):

     def send_message(self, data):
        print("Client sent '{}'".format(data))
The chat_client code has no awareness of the gui code but the gui code creates an instance of my "ChatClient" and is able to send messages to it. It is good design practice to not mix up GUI and 'back-end' functionality, this makes your code more testable and less subject to change if you were to change the layout of the GUI.

PS. I didn't change much of the gui code that you created but if I was doing this, I would make more use of objects and classes to do this.

Re: tkinter How to pass entry to another module?

Posted: Thu Nov 23, 2017 2:03 pm
by scotty101
Share the contents of and I might be able to help. should be in the same folder as the python program trying to import it.

Re: tkinter How to pass entry to another module?

Posted: Thu Nov 23, 2017 3:06 pm
by scotty101
If I copy the contents of your file and try to import it from another python script, I don't get any errors.
The only way I can replicate the same issue as you is to remove the code from

My suggestion would be to double and triple check that the python file name matches what you have on your "from irc_connection2 import IRC_Server" line.
Also perhaps you have another file with the same name that it is importing instead. To check which file you are importing, try creating a new python script in the same folder with the following code.

Code: Select all

import irc_connection2

For me this reports

Code: Select all

<module 'irc_connection2' from 'C:\\Python34\\Projects\\RaspberryPi\\IRC\\'>